# Literals, operators and primitive type casting

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

## Literals

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 Example Observations boolean true false There are only two possible values: true or false char ‘c’ ‘A’ ‘\$’ ‘\n’ ‘\100’ A character between single quotes or a letter or number after the escape character \ int 777 By default an integer is a int literal long 777l 777L For an integer to be long we use l or L float 3.14f 3.14F For a decimal number to be float we use f or F double 3.14 By default, a decimal number is a double literal

## Numeric Operators

 Operator Observations. +, -, * 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.

Example:

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

## Logical Operators

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

• ! negation (not): it gives the opposite result. Ej: !true is false and !false is true.

• && logical operators (and): it is only true if both are true.

• || logical operators (or): it is only false if both are false

Example:

```  		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:

• (int) 3.14 It converts the double number 3.14, into the int number 3

• (char)100.1f It converts the float number 100.1 into a char ‘d’

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

• 2 + 2.1 Java evaluates this expression turning the int 2 in double 2.0, then the result is double 4.1
• long l = 3; in this assignment the int 3 is converted to long
• char c = 100; in this assignment the int 100 is converted to char character 'd'

## 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"

Example:

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