What will you build

You will see the different type of primitive in Java

What You'll Learn

What will you need

Java has eight primitive types (byte, short, int, long, float, double, char, boolean). This include four integer types and two floating-point types.

In Java, everything is an object, exception of these 8 primitives. They have no methods or other properties associated to it, and the declaration is very simple, it doesn't need to instantialise an object. The value is directly assigned to the variable.

All primitive have equivalent Wrapper classes (see Codelabs Wrapper classes for more details).

PrimitiveSource of the image (C. Horstamn: Big Java)

We can distinguish a primitive from an object, as Object should always start with an Upper case, primitive start with a lower case.

In Java, strings are objects that belong to the class String, and it is therefore not a primitive.

The wrapper class of the primitive start with an Upper case. For example, the wrapper class of int is Integer.

Java has 4 primitive that defined integer: byte, short, int, long.

Byte can contains only one byte, short 2 bytes, int 4 bytes and long 8 bytes. For longer integer, Java did define the BigInteger, AtomicInteger classes (which are not primitive).

Depending of the space you will need to store the value, you should choose the adequate type of primitive. Often developpers will use int for everything. However, if you have billions of data, and this data will always between -128 and 127, why should we waste memory?

In the file Primitive.java, implement the following class and methods. For this exercise, we are returning the value that is defined in the method. This shows not only the type of primitives, but also how to return a variable in Java.

public class Primitive{


	public int returnInt(){
		int a = 5;
		return a;
	}
	
	public byte returnByte(){
		byte a = 1;
		return a;
	}
		
	public short returnShort(){
		short a = 2;
		return a;
	}
	
	public long returnLong(){
		long a = 10;
		return a;
	}

}

In a second class (so file), implement the class TestPrimitive and the method to test your Primitive class.

public class TestPrimitive{

	public static void main (String [] args){
	Primitive p = new Primitive();
	System.out.println("int "+p.returnInt());	
	System.out.println("byte" +p.returnByte());	
	System.out.println("short "+p.returnShort());	
	System.out.println("long "+p.returnLong());	
	}
}

Compile and run the TestPrimitive.java.

In java, two primitive type of decimal exists: float and double. The same than for Integer, you should use the type of variable you want to use accordingly to your needs, and not always use Double.

In the file Primitive.java, implement the following methods.

To declare you want a float and not a double, the f is required as if not, the value will be stored as a double, and this method will not compile, as you are returning a double.

	public float returnFloat(){
		float f = 3.9f;
		return f;
	}

If you declare a float on the assignment side of the variable declaration, but return it in a double, it will return the full representation of the double.

	public double returnDoubleDefinedWithF(){
		double d = 3.9f;
		return d;

	}

	public double returnDouble(){
		double d = 3.9;
		return d;

	}

In a the second class TestPrimitive continue to invoke in the main (created above) the methods from the Primitive class that you just implemented.

	System.out.println("float "+p.returnFloat());
	System.out.println("double defined with a f " +p.returnDoubleDefinedWithF());		
	System.out.println("double " +p.returnDouble());
	System.out.println("\n");	

Compile and run the TestPrimitive.java.

You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git