Literals, operators and primitive type casting

In this tutorial we will see the handling of primitive types, constants or literals, operators and casting.


A literal is a constant value defined in the code. For example, the number 777.

In the table below, we see examples of different types of literals.

Primitive Type




true false

There are only two possible values: true or false


‘c’ ‘A’ ‘$’ ‘\n’ ‘\100’

A character between single quotes or a letter or number after the escape character \



By default an integer is a int literal


777l 777L

For an integer to be long we use l or L


3.14f 3.14F

For a decimal number to be float we use f or F



By default, a decimal number is a double literal

Numeric Operators



+, -, * and /

Addition, subtraction, multiplication and division. It can be used with all numeric types, even between different numeric types. E.g.: 2 + 3.14 = 5.14


This operator returns the remainder of a division. E.g.: 10% 3 = 1

++ and --

Increment and decrement: It can only go before and after a variable. It modifies the value of the variable and returns the content of the variable. If used before the variable, it returns the incremented or decremented value, if it is used after, it returns the unchanged value. E.g.: ++i, i++,  

Relational Operators

These operators take two numeric values ​​or char and return a boolean value. These are: == equal, != not equal, <less than,> greater than, <= less than or equal and > = greater or equal.


		boolean b1 = 100>200; // false
		boolean b2 = 100<=200; // true

Logical Operators

They receive and return boolean values. The logical operators are:


  		boolean b1 = !(100>200); // true
		boolean b2 = 100<=200 && false; // false

Primitives types casting

The transformation of a value from one type to another type, is known as casting. To convert a value to a desired type, we place the type between parentheses, before the value. Examples:

Under certain conditions there is an automatic casting in assignments and expressions:

Concatenate Strings with operator +

String is not a primitive type in Java, but it is not a normal object class either. We can create String literal using quotes. E.g.: "Hello world", "$ 1000 pesos".

The + operator applied to String objects, means concatenation. E.g.: "Hello" + "edu" = "Hello edu".

If we use + between a number and a String object, automatic casting is performed. E.g.: "PI Number =" + 3.14 = "PI Number = 3.14"


  		double d = 1.3;
		char c = 100;
		boolean b = true && c > 200 || 3.14 < d;
		System.out.println("b=" + b); // print "b=false"