Data Types and Control Flow Notes
Data Types and Control Flow Notes
Data Types:
In Java, data types are used to define the type of data that a variable can hold. They help the
compiler understand the kind of operations that can be performed on the data and allocate
memory accordingly. Java has two categories of data types: primitive data types and
reference data types.
Note:
Except Boolean and char all remaining data types are considered as signed data types
because we can represent both "+ve" and"-ve" numbers.
Byte
Negative Numbers:
For negative numbers, the sign bit is 1, indicating negativity.
To represent negative numbers using two's complement, you invert the bits of the
positive counterpart (flipping 0s to 1s and 1s to 0s) and add 1 to the result.
In the case of byte, the two's complement of 0000000 (binary for 0) is 10000000,
which represents -128.
Putting it all together:
Positive values use 7 bits to represent the range from 0 to 127.
Negative values use 7 bits to represent the range from -1 to -128.
Note : byte data type is best suitable if we are handling data in terms of
streams either from the file or from the network.
Example:
byte b=10;
byte b2=130;//C.E:possible loss of precision
byte b=10.5;//C.E:possible loss of precision
byte b=true;//C.E:incompatible types
byte b="ashwani";//C.E:incompatible types
Short:
The most rarely used data type in java is short.
Size: 2 bytes
Range: -32768 to 32767(-215 to 215-1)
Example:
short s=130;
short s1=32788;//C.E:possible loss of precision
short s2=true;//C.E:incompatible types
Int:
Example:
int i=123;
int j=9.6;//C.E:possible loss of precision
int k=true;//C.E:incompatible types
int l=false;//C.E:incompatible types
long:
Why log required if have int data type?
Whenever int is not enough to hold big values then we should go for
long data type.
Example:
int smallNumber = 1000;// Suitable for small to medium-sized values
long bigNumber = 100000000000L; // Used for larger values
float:
Uses Size: 4 bytes to store a floating-point number.
Suitable If we want to 5 to 6 decimal places of accuracy then we should go for float.
Example: float pi = 3.14159f;
double:
what is precision?
Precision refers to the level of detail or accuracy in representing numbers,
especially decimal numbers. In the context of floating-point numbers, precision
determines how many significant digits can be reliably stored and manipulated.
For example, let's consider the decimal number 1/3, which is a repeating fraction in
decimal form: 0.333333...
Single-Precision (float):
If we store 1/3 as a float, the limited precision of 32 bits means that only a certain
number of decimal places can be accurately represented. The result might be
something like.
float oneThird = 0.33333334f;
Here, the precision is limited due to the available bits, and the value is rounded after
a certain number of decimal places.
Double-Precision (double):
Using a double, with its 64-bit precision, allows for more accurate representation:
double oneThird = 0.3333333333333333;
In this case, the greater precision of the double allows more decimal places to be
stored accurately.
Note:
In old languages like C & C++ are ASCII code based the no.Of ASCII code characters
are < 256 to represent these 256 characters 8 - bits enough hence char size in old
languages 1 byte. In java we are allowed to use any worldwide alphabets character
and java is Unicode based and no.Of unicode characters are > 256 and <= 65536 to
represent all these characters one byte is not enough compulsory we should go for
2 bytes.
Control Flow
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
else-if Ladder:
Syntax:
switch(x)
{
case 1:
action1
case 2:
action2
.
.
.
default:
default action
}
Note: Until 1.4 version the allow types for the switch argument are
byte, short, char, int but from 1.5 version on wards the corresponding
wrapper classes (Byte, Short, Character, Integer) and "enum" types also
allowed.
Examples
public class SwitchExmp{
public static void main(String args[]){
int x=10;
switch(x)
{
System.out.println("hello");
}}}