Java – Primitive Type

The simplest types in java are call primitive type. Java has 8 built-in primitive types, and each of these has default value when not declare in a method.

  • boolean, 8-bits, default false
  • byte, 8-bits default 0
  • short, 16-bits default 0
  • int, 32-bits default 0
  • long, 64-bits default 0
  • float, 32-bits default 0.0
  • double, 64-bits default 0.0
  • char, 16-bits default blank

Primitive type declare as class attributes

When primitive type declare as class attributes, it will return a default value when they are not initialize.

public class PrimitiveAttribute {
   private boolean isTrue;
   private byte myByte;
   private short myShort;
   private int myInt;
   private long myLong;
   private float myFloat;
   private double myDouble;
   private char myChar;

   // all getter method
   public static void main(String[] args) {
      PrimitiveAttribute obj = new PrimitiveAttribute();
      System.out.println(obj.isTrue);
      System.out.println(obj.getMyByte());
      System.out.println(obj.getMyShort());
      System.out.println(obj.getMyInt());
      System.out.println(obj.getMyLong());
      System.out.println(obj.getMyFloat());
      System.out.println(obj.getMyDouble());
      System.out.println(obj.getMyChar());
   }
}

##    
false
0
0
0
0
0.0
0.0
<blank>

Initialization

Declaration variables in Java method must be initialize before we can use, otherwise it will show compiler. However when it declare in a class attribute, such as global, private static variable. it will initialize as null for object and 0 to number, false to boolean, 0.0 to decimal value, empty to char.

Primitive type declare as local variable

When primitive type declare as local variable, the variable must be declare and initialize with value. Otherwise it will get compilation error.

Underscore Syntax

A underscore value can make large number to be more readable. It mainly improved the human eyes and java compiler will remove them. However, certain rules still apply.

  1. Underscore has to be located between digits (integer)
  2. Underscore has to be located between digits but not start or end with decimal (double/float)
int bignumber = 100000000;
int bignumber2 = 100_000_000; // make easy to read

double valid1 = 1.2345F; // this value automatic convert to double
double valid2 = 100_333.03;
double valid3 = 100333.03_329;

double valid4 = 6.000123E04;
double valid5 = 6.000_123E04;

double notCompile1 = 10_.25;
double notCompile2 = 10._25;
double notCompile3 = _10.25;
double notCompile4 = 10.25_;
double notCompile5 = 6.000_123_E04;

Block and Scope

In line 21, this line of code will not compile, due to cannot access to block local variable.

1 public class BlockAndScope {
2
3  static int staticVariable = 5;
4  
5  public static void main(String args[]) 
6  {
7     int localVariable = 50;
8     
9     { 
10         int blockLocalVariable = 60;
11         {
12            int innerBlockVariable = 70;
13         }
14         System.out.println("staticVariable " + staticVariable);
15         System.out.println("localVariable " + localVariable);
16         System.out.println("blockLocalVariable " + blockLocalVariable);
17     }    
18     
19     System.out.println("staticVariable " + staticVariable);    
20     System.out.println("localVariable " + localVariable);
21     //System.out.println("blockLocalVariable " + blockLocalVariable);  
22
23    int innerBlockVariable = 90;
24    System.out.println("innerBlockVariable " + innerBlockVariable);
22  }
20}

Understand Null

By default, all java object is initialize as null.

package com.codeomitted;

public class UnderstandNull {
	
  static Object obj1 = new Object();
  static Object obj2 = null;
  static Object obj3;
	
  public static void main(String[] args) {
    System.out.println(obj1);
    System.out.println(obj2);
    System.out.println(obj3);
		
    Object obj4 = new Object();
    Object obj5;
    System.out.println(obj4);
                
    //This line will not compile due to local variable has not been initialized
    //System.out.println(obj5);
		
  }
}


# Output 
java.lang.Object@4d591d15
null
null
java.lang.Object@65ae6ba4

Floating Type

Floating point is not suitable for financial calculation due to it’s roundoff errors cannot be tolerated. See below example:

public class FloatProblem {

  public static void main(String[] args) {
    float a = 2.0f;
    float b = 1.2f;
		 
    System.out.println(a - b);
  }
}
## Output ##
0.79999995

Char Type

Character code unit can be express in hexadecimal prefix with ‘\u’ eg \u004A or \uD840. In Java, It’s use UTF-16 character encoding.

Boolean Type

Java is strong type programming language, in boolean type only have true and false 2 values. There is no direct relationship with Java boolean type and number type such as integer 0 and 1.

Java – Primitive Type

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.