Published by: sadikshya
Published date: 16 Jun 2021
Assigning a value of one type to a variable of another type is known as Type Casting. Operator precedence is applicable in typecasting. In other words, typecasting is a way to convert a variable from one data type to another data type. For example, if you want to store a ‘long’ value into a simple integer then you can type cast ‘long’ to ‘int’. You can convert the values from one type to another explicitly using the cast operator
Widening or Automatic type conversion
Automatic Typecasting takes place when,
– the two types are compatible
– the target type is larger than the source type
Narrowing or Explicit type conversion
When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit typecasting.
Operator precedence describes the order in which operations are performed when an expression is evaluated. Operations with higher precedence are performed before those with lower precedence. For example, multiplication is performed before addition. In other words, operator precedence determines the way in which operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence.
Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression assigns a value to a variable. Expressions are built using values, variables, operators, and method calls.
Let’s take an example,
int score; score = 90;
Here, score = 90 is an expression that returns int.
Double a = 2.2, b = 3.4, result;
result = a + b – 3.4;
Here, a + b – 3.4 is an expression.
if (number1 == number2)
system.out.println(“Number 1 is larger than number 2”);
Here, number1 == number2 is an expression that returns Boolean. Similarly, “Number 1 is larger than number 2” is a string expression.
A string is a data type used in programmings, such as an integer and floating-point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word “hamburger” and the phrase “I ate 3 hamburgers” are both strings.
Example:
Create a variable of type String and assign it a value:
String greeting = “Hello”;
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets:
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Example:
String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
System.out.println(cars[0]);
A control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement or decides which of two statements to execute. A loop decides how many times to execute another statement.Java Control statements control the order of execution in a java program, based on data values and conditional logic.
Java If-else Statement
The Java if a statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.
if statement
if-else statement
if-else-if ladder
nested if statement
Repeating the same code fragment several times until a specified condition is satisfied is called iteration. Iteration statements execute the same set of instructions until a termination condition is met.
If a loop exists inside the body of another loop, it’s called a nested loop. Here’s an example of nested for loop.
for (int i = 1; i <= 5; ++i) {
// codes inside the body of outer loop
for (int j = 1; j <=2; ++j) {
// codes inside the body of both outer and inner loop
}
// codes inside the body of an outer loop
}
Here, a for loop is inside the body another for loop.
It should be noted that you can put one type of loop inside the body of another type. For example, you can put a while loop inside the body of a for-loop.