Java Basics – Static and Initializer Block, Type Casting, String Handling

The Static and Initializer Block

In Java programming language, the static keyword is used for memory management mainly. The static keyword is used along with variables, nested class, methods, and blocks. It is a set of statements in a program that are executed by the Java virtual machine before the main method. When the class loading starts and if the programmer wants to perform a specific task then he or she can define the task inside this static block. This task will be then executed when the class will be loading. Besides, we can define as many static blocks in a class as we want and these blocks will be always executed from the top to the bottom. Let us understand this with the help of an example.

When we execute a program, the static block is executed before the main method. All the statements that have been written inside this static block will be executed first. However, both are static.

Class Staticexample

{

Static

{

System.out.println(“Welcome everyone”);

System.out.println(“This is a static block”);

}

Public static void main(String args[])

{

System.out.println(“This is the main() method”);

}

}

Output –

Welcome everyone

This is a static block

This is the main() method

Initializer

Now, on the other hand, the initializer block in Java is used to initial all the instance data members. This block is executed whenever an object is developed or created. The initializer block is basically copied into the Java compiler and then it is copied to every constructor. The initialization block is then executed before the code in the constructor. We can have both the static as well as the initializer block in the Java program. But the static block will execute first before the initializer block.

class Initializer{

{

System.out.println(“Welcome everyone.”);

System.out.println(“This is the Initializer block”);

}

public Initializer()

{

System.out.println(“Default Constructor is invoked”);

}

public static void main(String args[])

{                        Initializer obj = new Initializer();

System.out.println(“This is the main() method”);

}}

Output –

Welcome everyone.

This is the Initializer block

Default Constructor is invoked

This is the main() method

Type Casting

In Java, type casting is a process or method that transforms a data type into another data type. It transforms them in both ways are manually as well as automatically. The compiler does the automatic conversion and the programmer performs the manual conversion. In this section, we will not discuss type casting and its type with some examples.

It is clear from the above section that converting a value from one data type to another is type casting. Type casting is of two types that is the widening type casting and narrowing type casting.

Widening Type Casting –

Converting or transforming a lower data type into a higher data type is called the widening type casting. However, the widening type casting is also known as the implicit conversion or casting down. It can be done automatically and it is safe as there is no chance to lose all your data. It takes place when:

  • Both the data types should be compatible with one another.
  • The type target should be larger than the source type.

For example, if you want to convert between numeric data types to a character or boolean then it cannot be done automatically. The Boolean and character data type is also not compatible with one another.

public class WideningExample

{

public static void main(String[] args)

{

int x = 7;

//automatically converts integer type into long type

long y = x;

//automatically converts long type into float type

float z = y;

System.out.println(“Before conversion, int value “+x);

System.out.println(“After conversion, long value “+y);

System.out.println(“After conversion, float value “+z);

}

}

Output –

Before conversion, the value is: 7

After conversion, the long value is: 7

After conversion, the float value is: 7.0

Narrowing Type Casting –

Converting or transforming a higher data type into a lower data type is the narrowing type casting. Although, it is also called the explicit conversion or casting up. The programmers do this type of type casting manually. If type casting is not performed then the compiler reports a compile-time error. In the below example, we have performed this type casting twice. First, we converted the double type into long, and then the long data type was converted into the int data type.

public class NarrowingExample

{

public static void main(String args[])

{

double d = 166.66;

//converting the double data type into the long data type

long l = (long)d;

//converting the long data type into the int data type

int i = (int)l;

System.out.println(“Before conversion: “+d);

//fractional part lost

System.out.println(“After conversion into long type: “+l);

//fractional part lost

System.out.println(“After conversion into int type: “+i);

}

}

Output –

Before conversion: 166.66

After conversion into long type: 166

After conversion into int type: 166

String Handling

Strings are widely used in the Java programming language. Also, strings are a sequence of characters. In this language, we usually treat strings as objects. Moreover, the Java programming language platform offers the string class to develop and manipulate the strings. Therefore, the direct way to generate a string is to write the below code –

String example = “Hey, my name is Mark Joseph.”;

Whenever the compiler detects a string in the code, it creates a string object with its given value in the case, “Hey, my name is Mark.”

Example –

Public class String {

Public static void main(String args[]) {

Char[] = markArray = {‘m’,’a’,’r’,’k’};

String markString = new String(markArray);

System.out.println(markString);

}

}

The output will be – mark

Leave a Comment