Primitive and non primitive data type in java

To do :

  • System.out.println(10*20*30*500000000*60.0);//1.79999999E14 what does E signify
  • Type casting, widening data type (
  • int amount = 0b101;
  • Object type and primitive type which is better and which situations.
  • memory allocated in case of non primitive
  • Speed of access for non primitive data type
  • Precision of float and double
  • Why we need to added f and l at the end of float and long respectively.
  • How to add two bytes a = 100 and b = 200 will result into -56 ? (cause of 2nd compliment)
  • You can add _ in numerical literals to make it more readable
    int million2 = 1_000_000;
    System.out.println(56); // 56
    System.out.println(0b11); // 3
    System.out.println(017); // 15
    System.out.println(0x1F); // 31
TypeSize in bits(in Ram)Min ValueMax ValuePrecisionExample
booleanJVM specific(1 bit)--true or falseboolean x =true;
byte8-128127+127 or -127
byte b = 120;
char1602^16 -1All unicode characterschar c ='A';
char c = 65;
short16-2^152^15-1+32,767 to -32,768short s = 30000;
int32-2^312^31-1+2,147,483,647 to -2,147,483,648int i = 10;
long64-2^632^63-1+9,223,372,036,854,775,807 to -9,223,372,036,854,775,808long l = 90;
float322^-149(2-2^-23).2^1273.402,823,5 E+38 to 1.4 E-45 float f = 65f;
double642^-1074(2-2^-52).2^10231.797,693,134,862,315,7 E+308 to 4.9 E-324double d = 65.55;
  • Java has eight built-in data types, referred to as the Java primitive types.
  • These eight data types represent the building blocks for Java objects, because all Java objects are just a complex collection of these primitive data types.

Difference between Primitive and Non primitive data type is :

  • Primitive data hold a value and cannot be set to null while non primitive can do that.
  • When a non primitive data type reference is not null then a method of that class can be called using that object reference but primitive data type cannot do that.

Identifiers :

  • Java has precise rules about identifier names.
  • Luckily, the same rules for identifies apply to anything you are free to name, including variables, methods, classes, and fields.

There are only three rules to remember for legal identifiers:

  • The name must begin with a letter or the symbol $ or _ .
  • Subsequent characters may also be numbers.
  • You cannot use the same name as a Java reserved word.

Legal Identifiers :


IIlegal identifiers :

3DPointClass // identifiers cannot begin with a number
hollywood@vine // @ is not a letter, digit, $ or _
*$coffee // * is not a letter, digit, $ or _
// public is a reserved word

Default initialization of variables :

Note : A local variable no matter the data type, be it primitive or non primitive or object they cannot be set to default value on the other hand global variable can have a default value, note a compile time error is thrown when a local variable is not initialized and expected to perform operations on it default value.

Using an uninitialized local variable :

  • Local variables are those variable which are defined with in a method.
  • If a local variable is not initialized after declaration then java compiler will throw a compile time error saying that the variable is not initialized
  • Compile is also smart enough to identify a variable if they are only initalized in one of the if variable

Example 1 :

	public static void main(String[] args) {
		int x = 10;
		int y ;
		int z = x + y; //Compile time error : The local variable y may not have been initialized

Example 2 : Compiler is also smart to identify complex initializations

	public static void main(String[] args) {
		int x = 10;
		int y ;
		if(Boolean.parseBoolean(args[0])) {
			y = 10;
		int z = x + y;//The local variable y may not have been initialized

Using an uninitialized class variable and instance variable :

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  • Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Instance variables can be accessed directly by calling the variable name inside the class, but inside static method they should be called obj.instance way i.e fully qualified way.
  • Static variables can be accessed by calling with the class name ClassName.VariableName.
  • Instance variable has a copy for each object.
  • Class variable has only one copy across class.

Below are the default value of uninitialized class and instance variable :

Variable TypeDefault Initialization value
byte, short, int, long0
float, double0.0
char‘\u0000’ (null)
All object refenrencenull

Numeric Promotion in java

Rules of Numeric Promotion

  1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
  2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s data type.
  3. Smaller data types, namely byte , short , and char , are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int .
  4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

Example :

public class NumericPromotionDemo {
	public static void main(String[] args) {
		//Rule #1
			int x = 10;
			long y = 20;
			long z = x+y;// x automatically promoted to long
		//Rule #2
			int x = 10;
			float f = 20.2f;
			float z = x + f; // x is automatically promoted to float
		//Rules #3
			byte b = 10;
			short s = 20;
			int x = b + s; 
			//be it byte,char,short they are automatically promoted to int when arithmatic operator is applied
		//Rule #4
			byte b = 10;
			short s = 20;
			int i = 30;
			long l = 5;
			float f = 60;
                        //X automatically promoted to float
			float x = b + s * + i + l + f;

Overflow and Underflow

short y = (short)1921222;
// Stored as 20678 overflow example
// -2147483648 the integer wrapped around since int can store value more than that

The expressions in the previous example now compile, although there’s a cost. The sec-
ond value, 1,921,222 , is too large to be stored as a short , so numeric overfl ow occurs
and it becomes 20,678 . Overfl ow is when a number is so large that it will no longer fi t
within the data type, so the system “wraps around” to the next lowest value and counts
up from there. There’s also an analogous underfl ow, when the number is too low to fi t in
the data type

Adding Optional Labels

A label is an optional pointer to the head of a statement that allows the application fl ow to jump to it or break from it. It is a single word that is proceeded by a colon ( : ). For example,

int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}};
OUTER_LOOP: for(int[] mySimpleArray : myComplexArray) {
INNER_LOOP: for(int i=0; i<mySimpleArray.length; i++) {

Leave a Comment