Data Types

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Rules for Naming the Variables:

1) Variable name cannot start with a digit.


2) It can have lower case, upper case, digits in between or at the end, dollar symbol, underscore
and nothing else.
3) @, space or any other special character is not allowed.
4) Variable name is case sensitive.
5) Do not use reserved keywords (Predefined words).

Data Type:

 Integer Numbersffds
1) int −2 ×109 ¿ 2× 109
2) long  −8 ×10 18 ¿ 8 ×1018
3) byte
4) short

we can store int in a long but to store large variable in long we need to put L at the end. Even though the
number is with in the range of long we need to put L at the end.

 Decimal Numbers:
1) float  6 to 7 digits after decimal
2) double  15-16 digits after decimal

By default, the decimals are stored in a double, if the decimal is initialized by a float it shows error

 Boolean numbers:
1) It can store only true or false.
2) The default value of the Boolean is false.
3)

Type Casting:

Implicit conversion, small capacity variable to a large capacity variable, example int to long and explicit
conversion is the reverse.

In all explicit conversion it shows an error.

Note :
Decimal to non-decimal:

 Compiler shows error in decimal to non-decimal conversion, while non decimal to decimal
happen implicitly without any error.
 We need to do explicit type casting, but there will be some data loss. Digits after the decimal are
not shown.
 sc.next();  takes the string till the first space.
 sc.nextLine(); It takes whole line as input till enter is encountered

Rule of Mathematical Operations:

1) The ranking of operators is as follows:


a) Bracket
b) * and / are at same rank
c) + and – are at same rank
d) If two operators of same rank are present then the math operations happen from left to
right.
Example:
7 42
6 × = =7
6 6
In the above example first multiplication happens and then division happens.
7
× 6=1 ×6=6
6
In this example first division happens and then multiplication happens. (The output of division is
always quotient)
2) When we perform a mathematical operation between a decimal and a non-decimal the output
will be always a decimal number.
3) Whenever we perform operation between two operands of same category then the result is of
bigger category.
a) decimals  float and doble math operation  output will be double
b) integers  int and long math operation  output will be long

Operators:

Arithmetic Operators:

There are five math operators. (Plus, minus, multiplication, division, percentile).

 The division sign always gives the quotient because int/int always gives the int)
 Example 24/10 the result will be int so the answer is 2.4.

Relational Operations:

The result of the relational operations is a Boolean.


a>b, b>a, a<=b, a>=b, a==b, a! =b, etc.

Logical Operators:

&&

||

Unary Operators:

a++ and ++a

Post increment operator a++

Post operator will use the value of a in the current step and it increases the value of a by one for the
next step.

Pre-increment operators:

You might also like