6
6
Aim: Write a java program to illustrate the use of various primitive data types.
Theory: Java is statically typed and also a strongly typed language because, in Java, each type of data (such as
integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming
language and all constants or variables defined for a given program must be described with one of the Java
data types. Primitive Data Types: such as boolean, char, int, short, byte, long, float, and double.
Algorithm:
Step 1: Start.
Step 6: End.
Source Code:
Output:
Byte variable: 127
Char variable: A
Theory: The name Type Promotion specifies that a small size datatype can be promoted to a large size
datatype. i.e., an Integer data type can be promoted to long, float, double, etc. This Automatic Type
Promotion is done when any method which accepts a higher size data type argument is called with the smaller
data type.
Algorithm:
Step 1: Start.
Step 5: End.
Source Code:
Output:
Result 1: 1000
Result 2: 10000000
Result 3: 3140.0
Result 4: 3.14159E7
Aim: Write a java program to illustrate explicit type casting.
Theory: In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual conversion
performed by the programmer.
Algorithm:
Step 1: Start.
Step 6: End.
Source Code:
int intVar;
Output:
Theory: In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape
characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be
enclosed in quotation marks (""). These are the valid character literals. The Java compiler interprets these
characters as a single character that adds a specific meaning to the compiler.
Algorithm:
Step 1: Start.
Step 4: End.
Source Code:
System.out.println("Hello\tWorld"); // Tab
System.out.println("Hello\nWorld"); // Newline
System.out.println("Hello\bWorld"); // Backspace
System.out.println("Hello\\World"); // Backslash
Output:
Hello World
Hello
World
World
HellWorld
Hello
World
Hello"World
Hello'World
Hello\World
Aim: Write a java program to illustrate the application of modulus(%) operator. Suppose, your problem is to
display the current time at this moment in Indian Standard Time (IST).
Algorithm:
Step 1: Start.
Step 2: Obtain the total milliseconds since midnight, January 1, 1970, intotalMilliseconds
by invoking System.currentTimeMillis().
Step 9: Add an offset of 5 hours 30 minutes to obtain the current time in IST (since IST is 5 hours 30
minutes ahead of GMT).
Source Code:
import java.util.TimeZone;
currentHour += 5;
currentMinute += 30;
currentMinute -= 60;
currentHour += 1;
currentHour -= 24;
}
Output: