Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Primitive Data Types

The primitive data types are the most basic data type in Java. These are boolean, char, byte, short, int, long, float, and double. Lets discuss things using an example. In your Eclipse IDE, click on 'CoreJava' project and expand it. Click on the 'src' folder. Then, on your navigation bar, click 'File', then click 'New', then click 'Package'. Enter 'com.example.core' for the 'Name' text box.

Create New Java Package In Eclipse


Then click 'Finish' button.

Created Java Package In Eclipse


You can think about a package as a way of organizing your Java codes. The key thing in organizing your Java files according to packages is group them together with a package name that best describes your group of Java files. Click on that package and then create a new Java file and name it as PrimitiveDataTypes.java.
                            
  package com.example.core;

  /**
   * 
   * @author Rolan Liwanag
   * 
   * REFERENCE : 
   * https://docs.oracle.com/javase/tutorial/java/
   *     nutsandbolts/datatypes.html
   *
   * What is a variable? A variable is a name given by the  
   * person writing the code to represent a value. In   
   * Java, we declare a variable by stating the type  
   * followed by the name we wish to call it.
   * Example:
   * int abc;
   * 
   * Variable names are case-sensitive. A variable's name  
   * can be any legal identifier  an unlimited-length   
   * sequence of Unicode letters and digits, beginning 
   * with a letter, the dollar sign "$", or the  
   * underscore character "_". The convention, however,   
   * is to always begin your variable names with a letter,
   * not "$" or "_". Additionally, the dollar sign 
   * character, by convention, is never used at all. 
   * You may find some situations where auto-generated  
   * names will contain the dollar sign, but your 
   * variable names should always avoid using it. A 
   * similar convention exists for the underscore  
   * character; while it's technically legal to begin 
   * your variable's name with "_", this practice is 
   * discouraged. White space is not permitted.
   *
   */
  public class PrimitiveDataTypes {

    public static void main(String args[]) {
	  //Below are the Primitive Data Types in Java
		    
	  //8 bit signed no decimal point number ranging  
	  //from -128 to 127
	  byte aByte = 1;
	  System.out.println("A byte has no decimal points " 
	    + "and can be positive or negative number. " 
	    + "The aByte variable is equal to " + aByte);
		    
	  //a 16 bit signed no decimal point number ranging
	  //from -32,768 to 32,767
	  short aShort = 1;
	  System.out.println("A short has no decimal points " 
	    + "and can be positive or negative number. " 
	    + "The aShort is variable equal to " + aShort);
		    
	  //a 32 bit signed no decimal point number.
	  int anInt = 1;
	  System.out.println("An int has no decimal points " 
	    + "and can be positive or negative number. " 
	    + "The anInt variable is equal to " + anInt);
	    
      //a 64 bit signed no decimal point number. 
	  //This can store huge value.
	  long aLong = 1;
	  System.out.println("A long has no decimal points " 
	    + "and can be positive or negative number. " 
	    + "The aLong variable is equal to " + aLong);
		    
	  //a float is a 32 bit IEEE number with decimal
	  //points.
	  float aFloat = 1;
	  System.out.println("A float has decimal points " 
	    + "and can be positive or negative number. " 
	    + "The aFloat variable is equal to " + aFloat);
	      
	  //a double has decimal points and can be positive 
	  //or negative number. This can store a huge value.
	  double aDouble = 1;
	  System.out.println("A double has decimal points " 
	    + "and  can be positive or negative number. " 
	    + "The aDouble variable is equal to " + aDouble);
		    
	  //value is either true or false
	  boolean aBoolean = true;
	  System.out.println("A boolean is either true " 
	    + "or false. The aBoolean is equal to " 
	    + aBoolean);
		    
	  //a 16 bit UNICODE character.
	  char aChar = 'a';
	  System.out.println("A char only contains single " 
	    + "character. The aChar is equal to " + aChar);
	  /* For numeric types, why do we need to use one  
	   * type over another? The answer is depending on 
	   * the need (with decimal or not) and for memory
	   * saving reasons. The larger the capacity, 
	   * the larger memory it occupies.
	   */
	}
  }
                            



Back    Next