Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.1. Assignment operator example

3.6.1. Assignment operator example
Last updated: 27 January 2013.

Open your favorite text editor and write this sample in it. When you have finished, save the file as JavaOperators.java.

public class JavaOperators {

   
public void assign(){

         
int op1 = 0;
         
int op2 = 100;

          System.out.println
(op1); // print the value of op1 before the assignment

         
op1 = op2;

          System.out.println
(op1); // print the value of op1 after the assignment
   
}

}

The class JavaOperators has a single method named assign which has two local variables op1 (initialized to 0 via the assignment op1 = 0) and op2 (initialized to 100 via the assignment op2 = 100). The method prints the value of op1, then performs the assignment op1 = op2 and reprints the value of op1.

Yet you can't run the program because it doesn't have a main method. It is good programming style to create another class that will only contain the main method, instead of adding a main method to the class JavaOperators. For example, you can call that class Main or App. In this and subsequent tutorials, the class containing the main method will be named App (App is an abbreviation for Application).

Open your text editor, create a new file, write the sample below and save it as App.java in the same directory where the file JavaOperators.java resides.

public class App {

   
public static void main(String[] args) {

         
JavaOperators operators = new JavaOperators();

          operators.assign
();
   
}

}

The main method has two statements:
Compile the file App.java by typing the command javac App.java. If there are no compilation errors, two bytecode files are generated: App.class and JavaOperators.class. Run the generated App.class file via the command java App.

The output is:

0
100

The method assign provided by the class JavaOperators is working properly. The value of op1 has been replaced by the value of op2.

Actually, in the App class, the variable operators is useless because it is used only once. Consequently, the two statements of the class App can be merged into one statement:

public class App {

   
public static void main(String[] args) {

         
new JavaOperators().assign();
   
}

}

As you can see, I created an instance of the class JavaOperators without assigning it to a variable. As a result, the created instance does not have a name. Such a class is called an anonymous class.

Typically, a main method should have only one statement: a call to a method belonging to another class that starts your application. Writing the code of your application directly in the main method is not compliant with the Java object-oriented approach. All the processing has to take place within objects, each object providing functionalities of its own. Therefore, the main method should always be as concise as possible.

However, in some cases, the main method may have several statements preparing the call to the method that actually starts your application. For example, you may need to fetch command line arguments before starting your application and in such cases, the main method can have more than one statement. Nonetheless, the core purpose of the main method is to call a method that starts your application.


Primitive type casting

In some situations, you may need to assign an expression to a variable of a different primitive type (e.g. assigning an integer expression to a float variable). In Java, you can change the type of an expression to another type. Such an operation is called a cast. Every numeric primitive type can be cast to another numeric primitive type (byte, short, int, long, float or double). The primitive type boolean cannot be cast to another primitive type. As an example, the method assign2 shown below casts an integer to a float:
public class JavaOperators {

   
public void assign(){

         
int op1 = 0;
         
int op2 = 100;

          System.out.println
(op1); // print the value of op1 before the assignment

         
op1 = op2;

          System.out.println
(op1); // print the value of op1 after the assignment
   
}

   
public void assign2(){

         
int myInteger = 3;
         
float myFloat = (float) myInteger;

          System.out.println
(myInteger);
          System.out.println
(myFloat);
   
}

}

The class App must be modified to call the method assign2:

public class App {

   
public static void main(String[] args) {

         
new JavaOperators().assign2();
   
}

}

The output is:

3
3.0

You can get the integer part of a float or double expression simply by casting it to an integer. For example, take a look at the method assign3 shown below which casts a float to an integer:

public class JavaOperators {

   
public void assign(){

         
int op1 = 0;
         
int op2 = 100;

          System.out.println
(op1); // print the value of op1 before the assignment

         
op1 = op2;

          System.out.println
(op1); // print the value of op1 after the assignment
   
}

   
public void assign2(){

         
int myInteger = 3;
         
float myFloat = (float) myInteger;

          System.out.println
(myInteger);
          System.out.println
(myFloat);
   
}

   
public void assign3(){

         
float myFloat = 3.5F;
         
int myInteger = (int) myFloat;

          System.out.println
(myFloat);
          System.out.println
(myInteger);
   
}

}

If you run the method assign3, you will get this output:

3.5
3

The fractional part of the float variable has been truncated. Such a cast is sometimes termed a lossy cast. It may happen when you cast a primitive type into a smaller type (e.g. double to float, float to short, short to byte, etc).


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.1. Assignment operator example
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.2. Arithmetic operators examples

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7