SCJP – Primitive types / Operators / Format

Java Primitive Data Types
Type Values Default Size Range
byte signed integers 0 8 bits -128 to 127
short signed integers 0 16 bits -32768 to 32767
int signed integers 0 32 bits -2147483648 to 2147483647
long signed integers 0 64 bits -9223372036854775808 to 9223372036854775807
float IEEE 754 floating point 0.0 32 bits +/-1.4E-45 to +/-3.4028235E+38,
+/-infinity, +/-0, NAN
double IEEE 754 floating point 0.0 64 bits +/-4.9E-324 to
+/-infinity, +/-0, NaN
char Unicode character \u0000 16 bits \u0000 to \uFFFF0, 65535 (converted to int)
boolean true, false false 1 bit used in 32 bit integer NA

Table from here, with some explanations.

  • not possible to use instanceof with primitives int x=1; if (x instanceof Integer) //compilation fails:
  • Integer have a constructor: Integer(String s)

Wrappers and Autoboxing

  • unboxing a null will throw exception at runtime.
  • for small values wrappers created through boxing will be pooled. For numbers (int, short, long) until 127
    • the == comparison will be true
    • not if we create the Integer explicitly by the constructor
  • A literal is always boxed first. Then it is asigned to a variable.

Conversion issues - loss of precision

Operation error?
Float c = 4; Error cannot widen then boxing Float c = 4f;
Float d = 4.0; Error conversion from double to float - loss of precision Float d = 4.0f;
float f1 = 4; ok float can handle int - floating point representation
float f2 = 4.0; Error conversion from double to float - loss of precision float f2 = 4.0f;
Double a1 = 4; Error cannot widen then boxing Double a1 = 4d;
Double a2 = 4.0; ok double to double
double d3 = 4; ok double can handle int
double d4 = 4.0; ok double to double
Double a1 = 4f; error cannot widen then boxing Double a1 = 4d;
  • widen then box don't compile. byte b=5; go(b) compilation error with void go(Long x). This is just too much for the compiler
  • Boxing operation followed by a widening (simply superclass issue) operation it's ok for the compiler
    • byte b=5; go(b); void go(Object o)
  • new java.lang.Byte(4); //compilation error - Byte have no constructor with int


with overload

  • compiler will always choose the oldest technology (Java version)
  • the compiler will choose widening over boxing. int i=5; go(i) will prefer to match long instead of Integer
  • var-args method is "looser" than the other method, even against boxing (rule above)

Assignments rules between short, char, byte and int

  • all short, char, byte and int, can be handled same way
  • assignments with literals compiles if they are inside a range.
    • byte b = 'a'; //works
    • byte b = 'ã'; //compile error
    • assigns with operations between literals works fine if the resulting literal is in the range.
      • byte b = 'a' + 1; //works fine
  • assignments with expressions that contains at least on variable. (compiler don't know the value)
    • Expressions with + operator results to a int type.
    • should be assigned to a type that can hold a the int, other way only with a cast.
    • byte c=1; byte b = c+1 //compile error
  • int x =5; x=-x; //this compiles and is the negative variable value
  • (x++)*2  //only increments after the operator, even with ()


  • Operator == used between objects, the references must be the same type or hierarchy, other way it will give compiler error
  • Operator == between a primitive and a object, first the object is unboxed and then the primitives are compared
    • between 2 objects, for instance between two Integers, objects are compared are will result false
  • valueOf static method that returns an object instance
    • takes primitives or String, is box operation or conversion from String
  • parseXxx and xxxValue return primitives
    • parseXxx - takes String and returns primitives
    • xxxValue takes no argument, just unbox
  • both valueOf or parseXxx (the methods that takes as arguments a String), consider "011" not as octal but as decimal.
method class static? returns args
valueOf String static String any primitive
valueOf Integer static Integer int or String
valueOf Float static Float float or String
parseInt Integer static int String
parseFloat Float static float String
intValue Integer int -
floatValue Float float -



  • every primitives( byte,short,int,long) in an expression is first converted into int then the expression is evaluated
    • if we are assigning the result of the expression to narrower primitives(like byte or short) without explicitly typecasting it then it will cause compiler error.
  • float/0 --> Infinity or -Infinity
  • int/0 --> ArithmeticException
  • Infinity / Infinity --> NaN




%[arg_index$][flags][width][.precision]conversion char

  • "-" Left justify this argument
  • "+" Include a sign (+ or -) with this argument
  • "0" Pad this argument with zeroes
  • "," Use locale-specific grouping separators (i.e., the comma in 123,456)
  • "(" Enclose negative numbers in parentheses


  • b boolean
  • c char
  • d integer
  • f floating point
  • s string


String s = "123.456xyz";
NumberFormat nf = NumberFormat.getInstance();


  • nf.parse(s);
    • Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
  • nf.format(s);
    • To format a number for the current Locale ---
    • May throw an exception if founds not numbers as input


  • java.text.DateFormat
  • java.util.Date
  • java.util.Locale
  • DateFormat.getInstance() - doesn't receive parameters
  • DateFormat.getDateInstance(DateFormat.FULL) Other args: DateFormat.FULL, SHORT, LONG,...
  • DateFormat.getTimeInstance
  • DateFormat.getTimeInstance(DateFormat.FULL)
Locale[] la = { new Locale("it", "IT"), new Locale("pt") };
Date d = new Date();
DateFormat df = DateFormat.getInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, l[0]);

Calendar operations

Calendar cal1 = new GregorianCalendar(2009,5,5);
Calendar cal2 = new GregorianCalendar(); 

Date d = calendar.getInstance().getTime();   // the same as new Date();

cal1.roll(Calendar.MONTH, true);

System.out.println("Year: " + cal1.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, Calendar.MAY);  

Date date = cal1.getTime();


String, StringBuffer and StringBuilder

String StringBuffer / StringBuilder
concat X
contains X
format X
chatAt X X
indexOf X X
isEmpty X
lastIndexOf X X
length X X
replace X X
split X
subString X X
toLowerCase X
append X
delete X
insert X
replace X
toString X X
valueOf X

Bit operations

int x = 5;

x = x << 97;

  • << 1 moves all bits 1 position to the left. It's the same as move 1 positions and adds a 0 at the right. The same as multiply by 2. Example: 5 << 1 ---> 10
  • >>1 moves 1 bit to the right, and drops the most right bit
  • the shift operations by 97 its the same as rotate by 1 (for 32 bits variables), you can remove the 32 multiples.


  • >>> unsigned shift. Ignores the sign bit, means that rotates also the sign bit. rotate right by 1 a negative number results a big positive number
  • <<< doesn't exists
  • <<= left shift and assign: i<<=2 means i=i<<2, like in i++


  • 011 - in java represents an octal number. converted to decimal is 9.
  • 0x11 - in java represents hexadecimal number

Bitwise Operators

  • Bitwise operators compare two variables bit by bit, and return a variable
  • &, |, ^ - NOT IN EXAM

Logical Operators

  • &&
short-circuit AND The short-circuit feature of the && operator is so named because it doesn't waste
its time on pointless evaluations
  • ||
short-circuit OR
  • &
non-short-circuit AND non-short-circuit operators, they evaluate both sides
of the expression, always!
  • |
non-short-circuit OR
  • ^
exclusive-OR (XOR) EXACTLY one operand must be true ()non-short-circuit operator
  • !
boolean invert


Increment operator precedence (++)

  • return i++ // first returns the i value and then increments i
  • if ((i++ < 2) || i == 1) {} // in the second logical operation the i is already incremented
  • ++i or i++ only is different for the most inner logical operator where ++ is in.


  • The constructor Boolean(String a) will result in false for null or for any string other "TRUE" or "true"

Static utilities

  • Arrays.asList(aArray) - returns a ist backed by the given array:
    • changes affects both objects
    • the list cannot grow, it's backed by a fixed length array


Leave a Reply