Java Basic I

Variables

  1. 8 Primitive Variables
  • int width of 32-bit, [-2^32, 2^32 - 1]. After Java 7, a long integer can be written as -2_147_483_648 to be easier to read
  • byte width of 8-bit. [-2^8, 2^8 - 1]
  • short width of 16-bit, [-2^16, 2^16 - 1]
  • long width of 64-bit, [-2^64, 2^64 - 1]. Declare: long value = 100L
  • float width of 32-bit
    • float myFloatValue = 5.2f
    • float myFloatValue = (float) 5.2
  • double width of 64-bit
    • double myDoubleValue = 5.2d
    • double myDoubleValue = 5.2
    • Doubles are more preferred in java since it’s more precise and faster
  • char width of 16-bit (2 bytes)
    • char myChar = '\u00A9'
  • boolean

String is NOT a primitive data type in Java, but is considered as 9th variable

1
2
3
4
String str = "10";
int myInt = 50;
lastString = str + myInt;
System.out.println(lastString);

Output: 1050

[Cast]
Since Java will automatically translate literals in expressions into Integer, we have to cast it when doing calculations of other types: (byte) (myByteValue/2).Longer variables will accept shorter variables, (no need to cast byte to long), but shorter variables will not accept longer variables

Keywords & Expressions

Keywords are 53 reserved words.

Datatype is not part of the expression. Expression consist of variable, operators, value.