0% found this document useful (0 votes)
56 views

Inetsolv Core Java

Inetsolv core java day1

Uploaded by

bhanu_billa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Inetsolv Core Java

Inetsolv core java day1

Uploaded by

bhanu_billa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Core Java Day1:

1. Getting Started - Write your First Java Program

You should have already installed Java Development Kit (JDK). Otherwise, Read "How to
Install JDK and Get Started with Java Programming".

Let us begin by writing our first Java program that prints a message "Hello, world!" to the
display console, as follows:

Hello, world!

Step 1: Write the Source Code: Enter the following source codes using a programming text
editor (such as TextPad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for
Ubuntu); or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans).

Do not enter the line numbers (on the left panel), which were added to aid in the
explanation. Save the source file as "Hello.java". A Java source file should be saved with a
file extension of ".java". The filename shall be the same as the classname - in this case
"Hello".

1 /*

2 * First Java program, which says "Hello, world!"

3 */

4 public class Hello { // Save as "Hello.java"

5 public static void main(String[] args) {

6 System.out.println("Hello, world!"); // print message

7 }

8 }

Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable
bytecode "Hello.class" using JDK Compiler "javac". Start a CMD Shell (Windows) or Terminal
(UNIX/Linux/Mac OS X) and issue this command:

prompt> javac Hello.java

There is no need to explicitly compile the source code under IDEs (such as Eclipse or
NetBeans), as they perform incremental compilation implicitly.
Step 3: Run the Program: Run the program using Java Runtime "java", by issuing this
command:

prompt> java Hello

Hello, world!

Take note that the Run command is "java Hello" without the ".class" extension.

On IDEs (such as Eclipse or NetBeans), right-click on the source file ⇒ Run As... ⇒ Java
Application.

Brief Explanation of the Program

/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the
compiler. But they provide useful explanation and documentation to your readers (and to
yourself three days later). There are two kinds of comments:

1. Multi-line Comment: begins with /* and ends with */, and may span more than one
lines (as in Lines 1-3).

2. End-of-line Comment: begins with // and lasts until the end of the current line (as in
Lines 4 and 6).

public class Hello { ...... }


The basic unit of a Java program is a class. A class called "Hello" is defined via the keyword
"class" in Lines 4-8. The {...} is the body of the class. The keyword public will be discussed
later.

In Java, the name of the source file must be the same as the name of the public class with a
mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".

public static void main(String[] args) { ...... }


Lines 5-7 defines the so-called main() method, which is the starting point, or entry point for
program execution. The {...} is the body of the method which contains programming
statements.

System.out.println("Hello, world!"); [], {},()


In Line 6, the statement System.out.println("Hello, world!") is used to print the message
string "Hello, world!" to the display console. A string is surrounded by a pair of double
quotes and contain texts. The text will be printed as it is, without the double quotes.

2. Java Terminology and Syntax


Statement: A programming statement performs a piece of programming action. It must be
terminated by a semi-colon (;) (just like an English sentence is ended with a period) as in
Lines 6.

Block: A block is a group of programming statements enclosed by braces { }. This group of


statements is treated as one single unit. There are two blocks in this program. One contains
the body of the classHello. The other contains the body of the main() method. There is no
need to put a semi-colon after the closing brace.

Comments: A multi-line comment begins with /* and ends with */. An end-of-line comment
begins with // and lasts till the end of the line. Comments are NOT executable statements
and are ignored by the compiler. But they provide useful explanation and
documentation. Use comments liberally.

Whitespaces: Blank, tab, and newline are collectively called whitespace. Extra whitespaces
are ignored, i.e., only one whitespace is needed to separate the tokens. But they could help
you and your readers better understand your program. Use extra whitespaces liberally.

Case Sensitivity: Java is case sensitive - a ROSE is NOT a Rose, and is NOT a rose.
The filename is also case-sensitive.

3. Java Program Template

You can use the following template to write your Java programs. Choose a meaningful
"Classname" that reflects the purpose of your program, and write your programming
statements inside the body of themain() method. Don't worry about the other terms and
keywords now. I will explain them in due course.

1 public class Classname { // Choose a meaningful Classname. Save as "Classname.java"

2 public static void main(String[] args) {

3 // Your programming statements here!

4 }

5 }

4. Output via System.out.println() and System.out.print()

You can use System.out.println() or System.out.print() to print message to the display


console:

 System.out.println(aString) (Print-Line) prints the given aString, and brings


the cursor to the beginning of the next line.

 System.out.print(aString) prints aString but places the cursor after the printed string.
Try the following program and explain the output produced:

1 /* Test System.out.println() and System.out.print() */

2 public class PrintTest { // Save as "PrintTest.java"

3 public static void main(String[] args) {

4 System.out.println("Hello, world!"); // Advance the cursor to the beginning of next line after printing

5 System.out.println(); // Print a empty line

6 System.out.print("Hello, world!"); // Cursor stayed after the printed string

7 System.out.println("Hello,");

8 System.out.print(" "); // Print a space

9 System.out.print("world!");

10 System.out.println("Hello, world!");

11 }

12 }

Hello, world!

Hello, world!Hello,

world!Hello, world!

JAVA_HOME

C:\Program Files\Java\jdk1.8.0_20

JRE_HOME

C:\Program Files\Java\jre1.8.0_20

Path

JAVA_HOME\bin; JRE_HOME\bin;
JVM Architecture

JVM Architecture
JVM has various sub components internally. You can see all of them from the above diagram.

1. Class loader sub system: JVM's class loader sub system performs 3 tasks
a. It loads .class file into memory.
b. It verifies byte code instructions.
c. It allots memory required for the program.

2. Run time data area: This is the memory resource used by JVM and it is divided into 5 parts
a. Method area: Method area stores class code and method code.
b. Heap: Objects are created on heap.
c. Java stacks: Java stacks are the places where the Java methods are executed. A Java stack
contains frames. On each frame, a separate method is executed.
d. Program counter registers: The program counter registers store memory address of the
instruction to be executed by the micro processor.
e. Native method stacks: The native method stacks are places where native methods (for
example, C language programs) are executed. Native method is a function, which is written in another
language other than Java.

3. Native method interface: Native method interface is a program that connects native methods
libraries (C header files) with JVM for executing native methods.

4. Native method library: holds the native libraries information.

5. Execution engine: Execution engine contains interpreter and JIT compiler, which covert byte code
into machine code. JVM uses optimization technique to decide which part to be interpreted and which
part to be used with JIT compiler. The HotSpot represent the block of code executed by JIT compiler.

You might also like