Quick Reference :
- Rules for the name of identifiers
- Default initialization values of primitives on local and global scope
- Numeric Promotion
- Rule 1 : If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
- Rule 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.
- Rule 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 .
- Rule 3 : 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.
- Overflow and Underflow
- Overflow
- short y = (short)1921222;
- // Stored as 20678 overflow example
- System.out.print(2147483647+1);
- Underflow
- Underflow is the opposite of overflow. While we reach the upper limit in case of overflow, we reach the lower limit in case of underflow. Thus after decrementing 1 from Integer.MIN_VALUE, we reach Integer.MAX_VALUE.
- JVM does not throw any exception in case Overflow or underflow occurs, it simply changes the value. Its programmer responsibility to check the possibility of an overflow/underflow condition and act accordingly.
- Overflow
- Max Value int,float,byte,char,short,double & long can store
- byte = 8-bit integer
- int = 32-bit integer
- long = 64-bit integer
- short = 16-bit integer
- float – 32 bits
- double – 64 bits
- boolean – 1 bit
- char – 16 bits
- Always consider the max size of Integer variable, if long is possible for it or not
To do :
- System.out.println(10*20*30*500000000*60.0);//1.79999999E14 what does E signify
- Type casting, widening data type (https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2)
- 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
Type | Size in bits(in Ram) | Min Value | Max Value | Precision | Example |
---|---|---|---|---|---|
boolean | JVM specific(1 bit) | - | - | true or false | boolean x =true; |
byte | 8 | -128 | 127 | +127 or -127 | byte b = 120; |
char | 16 | 0 | 2^16 -1 | All unicode characters | char c ='A'; char c = 65; |
short | 16 | -2^15 | 2^15-1 | +32,767 to -32,768 | short s = 30000; |
int | 32 | -2^31 | 2^31-1 | +2,147,483,647 to -2,147,483,648 | int i = 10; |
long | 64 | -2^63 | 2^63-1 | +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 | long l = 90; |
float | 32 | 2^-149 | (2-2^-23).2^127 | 3.402,823,5 E+38 to 1.4 E-45 | float f = 65f; |
double | 64 | 2^-1074 | (2-2^-52).2^1023 | 1.797,693,134,862,315,7 E+308 to 4.9 E-324 | double 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 :
okidentifier $OK2Identifier _alsoOK1d3ntifi3r __SStillOkbutKnotsonice$ $$hell_o__
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 // 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 Type | Default Initialization value |
boolean | false |
byte, short, int, long | 0 |
float, double | 0.0 |
char | ‘\u0000’ (null) |
All object refenrence | null |
Numeric Promotion in java
Rules of Numeric Promotion
- If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
- 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.
- 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 .
- 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; System.out.println(x); } } }
Overflow and Underflow
short y = (short)1921222; // Stored as 20678 overflow example System.out.print(2147483647+1); // -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++) { System.out.print(mySimpleArray[i]+"\t"); } System.out.println(); }