Java programming
30.6K subscribers
131 photos
12 files
20 links
Download Telegram
Rules To Remember
There are a certain rules one must keep in mind while declaring a switch case in java. Following are a certain points to remember while writing a switch case in java.

1 We cannot declare duplicate values in a switch case.

2 The values in the case and the data type of the variable in a switch case must be same.

3 Variables are not allowed in a case, it must be a constant or a literal.

4 The break statement fulfills the purpose of terminating the sequence during execution.

5 It is not necessary to include the break statement, the execution will move to the next statement if the break statement is missing.

6 The default statement is optional as well, it can appear anywhere in the block.
public class Example{
public static void main(String args[]){
int month= 7;

switch(month){
case 1 :
System.out.println("january");
break;
case 2:
System.out.println("february");
break;
case 3:
System.out.println("march");
break;
case 4:
System.out.println("april");
break;
case 5:
System.out.println("may");
break;
case 6:
System.out.println("june");
break;
case 7:
System.out.println("july");
break;
case 8:
System.out.println("august");
break;
case 9:
System.out.println("september");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("november");
break;
case 12:
System.out.println("december");
break;
default:
System.out.println("not valid");
}
}
}

Output: july
Nested Switch Case
Nested switch case incorporates another switch case in an existing switch case. Following is an example showing a nested switch case.
Fall Through Switch Case
Whenever there is no break statement involved in a switch case block. All the statements are executed even if the test expression is satisfied. Following is an example of a fall through switch case.
Enum In Switch Case
Switch case allows enum as well. Enum is basically a list of named constants. Following is an example of the use of enum in a switch case.
String In Switch Case
After the release of Java 7, a switch case can have strings as a case. Following is an example of using string as cases in a switch statement.
The Coming chapter: JAVA CORE CONCEPT
JAVA CORE CONCEPT

What are the components of Java Architecture?
What
is Java Architecture?
Here, I will explain you the java architecture in simple steps.

- In Java, there is a process of compilation and interpretation.
- The code written in Java, is converted into byte codes which is done by the Java Compiler.
- The byte codes, then are converted into machine code by the JVM.
- The Machine code is executed directly by the machine.
Components of Java Architecture
There are three main components of Java language: JVM, JRE, and JDK.

Java Virtual Machine, Java Runtime Environment and Java Development Kit respectively.

Let me elaborate each one of them one by one:

Java Virtual Machine:
Ever heard about WORA? (Write once Run Anywhere). Well, Java applications are called WORA because of their ability to run a code on any platform. This is done only because of JVM. The JVM is a Java platform component that provides an environment for executing Java programs. JVM interprets the bytecode into machine code which is executed in the machine in which the Java program runs.
Explanation:

Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run the java program, class loader loads it first.

Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.

Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the application runs.

Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created for a single execution thread. The JVM stack of a thread is used by the thread to store various elements i.e.; local variables, partial results, and data for calling method and returns.

Native stack: It subsumes all the native methods used in your application.

Execution Engine:

- JIT compiler
- Garbage collector

JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in improving the performance of Java applications by compiling bytecodes to machine code at run time. The JIT compiler is enabled by default. When a method is compiled, the JVM calls the compiled code of that method directly. The JIT compiler compiles the bytecode of that method into machine code, compiling it “just in time” to run.

Garbage collector: As the name explains that Garbage Collector means to collect the unused material. Well, in JVM this work is done by Garbage collection. It tracks each and every object available in the JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:

Mark – it is where the garbage collector identifies which piece of memory is in use and which are not
Sweep – it removes objects identified during the “mark” phase.
Java Runtime Environment:
The JRE software builds a runtime environment in which Java programs can be executed. The JRE is the on-disk system that takes your Java code, combines it with the needed libraries, and starts the JVM to execute it. The JRE contains libraries and software needed by your Java programs to run. JRE is a part of JDK (which we will study later) but can be downloaded separately.

Java Development Kit:
The Java Development Kit (JDK) is a software development environment used to develop Java applications and applets. It contains JRE and several development tools, an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) accompanied with another tool.

The blue area shown in the diagram is JDK. Now, let me elaborate the development tools to you all.

java : it is the launcher for all the java applications.
javac : complier of the java programming languages.
javadoc: it is the API documentation generator.
jar: creates and manage all the JAR files.

Moving ahead with Java architecture, let us understand how Java platform is independent?

How is Java platform independent?
When is any programming language called as platform-independent? Well, if and only if it can run on all available operating systems with respect to its development and compilation.
Now, Java is platform-independent just because of the bytecode. Let me tell you what exactly is a bytecode? In simple terms,
Bytecode is a code of the JVM which is machine-understandable.
Bytecode execution in Java proves it is a platform-independent language.
Here, I will show you the steps involved in the process of java bytecode execution.