Java Data types code and rules

Primitive variables

Boolean

boolean result = false;

Only have two states, true or false.

Numeric (Integers and Floating point)

Integers made up of byte, int, short, long

type bits min/max value
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,483,647
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

You can store integers in four bases:

Base value range example
2, binary 0 and 1
int binaryValue = 0b1011;
8, octal 0 and 7
int octalValue = 0413;
10, decimal 0 and 9
int decimalValue = 10;
16, hex 0 and 9 and A to F
int hexValue = 0x1B;

To help, Java 7 allows the use of underscores in the values, making it more readable:

class Example_intergers {
	public static void main(String[] args) {
		long valueDecimal=101_256_202;
		System.out.println("101_256_202 == "+valueDecimal);  //101256202
		long valueOct=04_21;
		System.out.println("04_21 == "+valueOct);  //273
		long valueHex=0x20_CD_89;
		System.out.println("0x20_CD_89 == "+valueHex);  //2149769
		long valueBin=0b10_11;
		System.out.println("0b10_11 == "+valueBin);  //11
	}
}

There are rules for these underscores:

int lit1 = _100;  // Cannot start
int lit2 = 100_;  // or end with a underscore!

long lit3 = 999_L; //cannot place before suffix

long octV = 0_4; //cannot place straight after 0 prefix for Octal

long binV = 0b_1; //cannot place straight after 0b prefix
long hexV = 0x_CD; //cannot place straight after 0x prefix

int t = Integer.parseInt("1234_567"); //cannot use in string of digits

further examples:

//Which of these will work?
long v0 = 0_100_267_760; //This is fine
//long v1 = 0_x_4_13; //No - compile error == ';' expected, illegal underscore
//long v2 = 0b_x10_BA_75; //No - compile error == binary numbers must contain at least one binary digit, not a statement, illegal underscore
//long v3 = 0b_10000_10_11; //No - compile error == illegal underscore
//long v4 = 0xa10_AG_75; //No, not valid hex (G) - compile error == ';' expected
long v5 = 0x1_0000_10; //fine
long v6 = 100__12_12; //double __ but think it is still valid?

Floating-Point (float and double) numbers

type bits min/max value
float 32 +/-1.4E-45 to +/-3.4028235E+38
double 64 +/-4.9E-324 to +/-1.7976931348623157E+308

You can tell the compiler it is a float:

float v1 = 100.9824F;  //All floats must be declared with F or f
float v2 = 1833.23456f;
double v3 = 42356.23456;

//You can use scientific notation
double v4 = 123.1882e2;

//You can use those underscores again

float v5 = 1_000_000.45678f;

float v6 = 1_000_000._45678f; //cannot use next to decimal point
float v7 = 1_000_000_.45678F; //as above

float v8 = 1_000_000.45678_f; //cannot use next to suffix f or d

Character

Stores a single 16-bit Unicode character. Stored as a unsigned integer.

Examples are:

char v1 = 'T';
char v2 = 123;  //should be positive, negative fails to compile
char v3 = '\u0123';

variable names:

int EXAMPLE-V1; //won't compile because of hyphen
int example.v2; //won't compile because of '.'
int %example;   //won't compile because of %

int $example; //works
int _example; //works

Object reference variables

class Example {}

Example e0 = new Example();

//e0 is the object reference variable - which is the heap-memory address value to the object

int v0 = 100; //this value is stored in memory, rather than the reference

Operator assignment

class Assignment {

	static int v1 = 10;
	static int v2 = v1;

	static void showValues() {
		System.out.println("v1 == "+v1);
                System.out.println("v2 == "+v2);
	}

	public static void main(String[] args) {

		showValues(); // v1 == 10 v2 == 10

		v2 += v1;
		System.out.println("v2 += v1");
		showValues(); // v1 == 10 v2 == 20
		
		v1 = v2 = 10;
		showValues(); // v1 == 10 v2 == 10

		v2 *= v1;
		System.out.println("v2 *= v1");
		showValues(); // v1 == 10 v2 == 100

		v1 = v2 = 10;
		showValues(); // v1 == 10 v2 == 10

		v2 /= v1;
		System.out.println("v2 /= v1");
		showValues(); // v1 == 10 v2 == 1

	}
}

Invalid assignments

double v1 = true; //no can do!
boolean v2 = 'c'; //no can do!
boolean v3 = 0; //no can do!
boolean v4 -= v3; //no can do (you can't add or subtract booleans)

long v5 = 123456789012L;
int v6 = v5; //no can do

int v7 = 1234;
long v8 = v7; //all good

boolean v9 = true;
int v10 = v9; //no can do

More examples

public class ExampleAssignment {
	public static void main (String[] args) {
		boolean v1, v2, v3, v4, v5, v6; //all fine
		v1 = v2 = v3 = true; //all fine
		v4 = 0; //no can do!
		v5 = 'false' //no can do!
		v6 = yes; //no can do!
	}
}

Arithmetic operators

class Assignment {

	static int v1;
	static int v2;

	static double d1;
	static double d2;

	static void setIntValues() {
		v1 = 20;
		v2 = 10;
	}

	static void setDoubleValues() {
		d1 = 20.0;
		d2 = 10.0;
	}

	static void showIntValues() {
		System.out.println("v1 == "+v1);
                System.out.println("v2 == "+v2);
	}

	static void showIntValues(int v) {
		showIntValues();
		System.out.println("v3 == "+v);
	}

	static void showDoubleValues() {
		System.out.println("d1 == "+d1);
                System.out.println("d2 == "+d2);
	}

	static void showDoubleValues(double v) {
		showDoubleValues();
		System.out.println("d3 == "+v);
	}


	public static void main(String[] args) {

		setIntValues();
		showIntValues(); // v1 == 10 v2 == 20

		++v1;
		v2++;
		System.out.println("++v1 and v2++");
		showIntValues(); // v1 == 21 v2 == 11 - they are not part of an expression so behave the same way
		
		setIntValues();
		int v3 = v1 - ++v2;
		System.out.println("v1 - ++v2");
		showIntValues(v3); // v1 == 20 v2 == 11 v3 == 9

		setIntValues();

		v3 = v1 - v2++;
		System.out.println("v1 - v2++");
		showIntValues(v3); // v1 == 20 v2 == 11 v3 == 10

		

		setDoubleValues();
		double d3 = d1 * d2--;
		System.out.println("d1 * d2--");
		showDoubleValues(d3); // d1 == 20.0 d2 == 9.0 d3 == 200.0


		setDoubleValues();
		d3 = d1 * --d2;
		System.out.println("d1 * --d2");
		showDoubleValues(d3); // d1 == 20.0 d2 == 9.0 d3 == 180.0

		int z = 10;
		z = z++ + z + z-- - z-- + ++z;
		//equivalent to
		//10 + 11 + 11 - 10 + 10
		System.out.println("z == "+z);  //32

		z = 10;
		z = ++z + z + --z - --z + z++;
		//equivalent to
		//11 + 11 + 10 - 9 + 9
		System.out.println("z == "+z);  //32
		
	}
}

Relational operators

class RelationalExample {
	public static void main (String[] args) {
		int v1 = 10;
		int v2 = 20;

		long v3 = 10;
		long v4 = 20;

                boolean v5 = false;

		System.out.println("v1 >= v2 is "+(v1 >= v2)); //prints false

		System.out.println("v4 >= v3 is "+(v4 >= v3)); //prints true

		//Cannot compare different types, e.g. String versus long

		System.out.println("v1 == v2 is "+(v1 == v2)); //false
		System.out.println("v1 != v2 is "+(v1 != v2)); //true

		System.out.println("v5 == true is "+(v5 == true)); //false
		System.out.println("v5 != true is "+(v5 != true)); //true

		System.out.println("v5 == false is "+(v5 == false)); //true
		System.out.println("v5 != false is "+(v5 != false)); //false

		System.out.println("v5 = true is "+(v5 = true)); //true
		System.out.println("v5 = false is "+(v5 = false)); //false
	}
}

Logical operators

class LogicalExamples {
	public static void main(String[] args) {
		int v1 = 10;
		int v2 = 20;
		
		System.out.println("10 > 20 && 20 > 10 is "+(v1 > 20 && v2 > 10)); //prints false
		System.out.println("10 > 20 || 20 > 10 is "+(v1 > 20 || v2 > 10)); //prints true

		System.out.println("!(20 > 10) is "+(!(v2 > 10))); //prints false
		System.out.println("!(10 > 20) is "+(!(v1 > 20))); //prints true

		//Logical AND (&&) is true if ALL operands equates to true
		//Logical OR (||) is true if ONE operands equates to true
		//Logical NOT (!) is true if operand is false (i.e. the oposite)

		int v3 = 80;
		int v4 = 100;

		System.out.println(v3+", "+v4); //80, 100
		System.out.println("v4 < v3 AND ++v3 > 50 is "+(v4 < v3 && ++v3 > 50)); //false
		System.out.println(v3); //80
		System.out.println("v4 == 100 OR ++v3 > 100 is "+(v4 == 100 || ++v3 > 100)); //true
		System.out.println(v3); //80
		
		//Reason v3 does not get touched, lazy evaluation, once you know one you don't need to do the rest

		int a = 10;
		int b = 20;
		int c = 40;
		
		System.out.println(a++ > 10 || ++b < 30); //true
		 
		System.out.println(" a == "+a+", b == "+b+", c == "+c); //a will be 11, b 21, c40
		
		System.out.println(a > 90 && ++b < 30); //false a still 11 b not touched
		
		System.out.println(" a == "+a+", b == "+b+", c == "+c); //a will be 11, b 21, c40
		
		System.out.println(!(c>20) && a==10); //false a not evaluated
		
		System.out.println(" a == "+a+", b == "+b+", c == "+c); //a will be 11, b 21, c40
		
		System.out.println(a >= 99 || a <=30 && b == 10); //false
		
		System.out.println(" a == "+a+", b == "+b+", c == "+c);	//a will be 11, b 21, c40	
		
		System.out.println(a >= 99 && a <= 33 || b == 10); //false
		
		System.out.println(" a == "+a+", b == "+b+", c == "+c);	//a will be 11, b 21, c40	
	}
}

Operator precedence

Order is

  • Postfix
  • Unary
  • Multiplication
  • Addition
  • Relational
  • Equality
  • Logical AND
  • Logical OR
  • Assignment
  • int a = 10, b = 20, c = 30;
    System.out.println(a % b * c + a / b);
    
    /*
    
    (((a % b) * c)) + (a / b)
    (((10 % 20) * 30)) + (10 / 20)
    ( (10       * 30)) + (0)
    (300)
    
    */
    
    //You can use parentheses to change the order
    
    System.out.println(a % b * (c + a) / b);
    
    /*
    ((10 % 20) * ((30 + 10) / 20)
    ((10     ) * ( 40       / 20)
    ((10     ) * ( 2            )
    (20)
    */
    

    reference: http://www.manning.com/gupta/

    Tags: , ,

    Leave a comment