As we talked about abstract, for and continue keywords in our last article. We will be talking about the new, switch and assert keyword in Java.
new
When we create a class in Java and decide to use this class, we need to make an object of this class and using the object of the aforementioned class we control and use the components of the class.
An object of a class works like the handle of a motorbike. If you need to control the bike you need to hold the handle of this bike. I believe you have understood why we need to create an object.
You must be thinking if the object is so important, we need to know how to create an object of a class. We create an object of a class by using the ‘new’ keyword. The ‘new‘ keyword gives instructions to the processor to holds the memory in the system to store the object of this class.
Car.java Class Code
package newKeyword; public class Car { public String ColorOftheCar; public int HightOftheCar; public void StarttheEngine() { System.out.println("Engine is Running"); } }
Main Class
package newKeyword; public class Hello { public static void main(String[] args) { Car car = new Car(); System.out.println(car.ColorOftheCar = "Red"); System.out.println(car.HightOftheCar = 5); car.StarttheEngine(); } }
Explanation
As you can see in the above program. We created a class named Car and in that class, we created two variables to hold the color of the car and hight of the class. There is one function to start the engine of the car. We first created the variable of type Car class.
Car car
Then gave this variable a memory to hold the object of type Car class.
Car car = new Car();
Later on, the ‘car’ variable acted as the object of the class and we used this word to set the color and size of the car and invoked the method named car.StarttheEngine().
switch
The switch case is used to print one statement or execute one block of code based on the value of a variable. Let us take an example.
Example –
In a program, if the value of a variable ‘X’ is 1, the statement should be printed as ‘The value of X is 1’. If the value of variable ‘X’ is 2 the statement should be printed as ‘The value of X is 2’. If the value of ‘X’ is something else other than 1,2 then the statement should be printed as ‘The value of ‘X’ is something else.
Syntax
switch (key) { case value: break; default: break; }
Programs
public class Switch { public static void main(String[] args) { int X =3; switch (X) { case 1: System.out.println("The value of X is 1"); break; case 2: System.out.println("The value of X is 2"); break; default:System.out.println("The value of X is something else"); break; } } }
In the above program, we have just printed some statement but the same switch can be used to print one block of code as well by using the {code goes here}. Let us see the same program below.
public class Switch { public static void main(String[] args) { int X =3; switch (X) { case 1: { System.out.println("The value of X is 1"); } break; case 2: { System.out.println("The value of X is 2"); } break; default: { System.out.println("The value of X is something else"); } break; } }
As you can see in the above program, the program is the same but the only difference is the { } around the print statements. These { } creates a block which means the block can be used to execute a single block of code rather than printing just a statement if the condition is true.
Note – The switch statement can also take Strings as the case. Look at the program below.
public class Switch { public static void main(String[] args) { String X ="c"; switch (X) { case "a": { System.out.println("The value of X is a"); } break; case "b": { System.out.println("The value of X is b"); } break; default: { System.out.println("The value of X is something else"); } break; } } }
One question should come to your mind that Java is a case sensitive language and what will happen if someone has given upper-class String value. The whole program will fail. The switch has got that situation covered. You can add two cases of values. Look at the program below.
public class Switch { public static void main(String[] args) { String X ="c"; switch (X) { case "a": case "A": { System.out.println("The value of X is a"); } break; case "b": case "B": { System.out.println("The value of X is b"); } break; default: { System.out.println("The value of X is something else"); } break; } } }
Also, there is no upper limit on how many cases can be added here. The switch can take an ‘n’ number of cases. The default statement is for the situation when any of the case conditions are not met, the default statement or block of code will be executed.
Conclusion
In case you are wondering why we need the switch when the same thing can be achieved by if statements. You are absolutely correct but there are more chances of making a mistake when adding a lot of if conditions in if program. The switch is less complex some programmers say. Which one is simpler? This topic is debatable. We should understand both if and switch very well in order to be a good programmer.
The post new, switch and assert keywords in Java appeared first on LinuxAndUbuntu.
from LinuxAndUbuntu https://ift.tt/37DoKFl
0 Comments