NChapter 2 Introduction To Java Language
NChapter 2 Introduction To Java Language
NChapter 2 Introduction To Java Language
Introduction to
Java Language
Java Programming Language
• one of the most popular and widely used programming
languages written and developed by Dr. James Gosling and his
colleagues in early 1900 at Sun Microsystem, which is now
part of Oracle Corporation after it was completed on January
27, 2010
• released in 1995 as part of Sun Microsystem’s Java platform
• based primarily in terms of syntax and structure in C and C++
Brief History of Java
• James Gosling, Mike Sheridan, and Patrick Naughton initiated
the Java language project in June 1991.
• Originally designed for interactive television but it was too
advanced for the digital cable television industry at the time.
• Initially called oak after an oak tree that stood outside
Gosling’s office
Brief History of Java
• Sun Microsystems released the first public implementation as
Java 1.0 in 1995 and it promised “Write once, Run
Anywhere”(WORA)
• On November 13,2006,Sun released much of Java as free and
open-source software.
Types of Java Applications
1. Java Applet-usually stored on a website and is downloaded and run client
computer from within a web browser
2. Application- can run only on the computer.
3. JAR(Java Archive) File-is used to package java files together into a single file
4.Servlet-runs on a web browser and helps to display web pages
5. Swing application- used to build an application that has a GUI(windows,
buttons, menus, etc.)
6. EJB(Enterprise Java Beans)-runs on a web server and is used to develop large,
complex websites
Characteristics of Java
1. Java is simple
2. Java is object-oriented.
3. Java is distributed.
4. Java is interpreted.
5. Java is robust.
6. Java is secure.
7. Java is architecture- neutral
Characteristics of Java
8.Java is portable.
9. Java’s performance
10. Java is multithreaded.
11.Java is dynamic.
Benefits of Java
1. used to teach students how to program as a first language
2. Requires that each variable be initialized .
3. Requires that each method that declares a return type ,
always returns a value.
4. Java comes with a large set of classes and methods
Benefits of Java
5.Unlike C , Java primitive types, such as int, are always the
same size in the number of bits which helps achieve cross-
platform compatibility
6. Used to be thought of being slower than C, but less important
in recent years with computers being faster.
7. Java has exception handling that requires a programmer to
handle error-conditions such as input/output errors.
Benefits of Java
8. Code compiled on one Java platform can be run on other
platforms that support Java without modification of either
source code nor the byte-code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Example of Java Program
class Hello_ World{ //name of the program
public static void main(String[]args){
System.out.println(“Hello World.”);
System.out.println();
System.out.println(“End of Program”);
}
} // End of Code
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Tools Needed in writing a Java program
1. JDK-Java Development Kit – that can be downloaded
from www.oracle.com
2. Any Text Editor (Notepad, Eclipse,Netbeans, Textpad)
Java Syntax
• The syntax of the Java programming is the set of rules defining
how a Java program is written and interpreted.
• Syntax is mostly derived from C AND C++, Java is almost
exclusively an object-oriented language.
Variable Declaration
• Variable- is a value that can be changed depending on
conditions or on information passed to the program
• In Java programming, u can define a variable with the
following form:
Data Type Variable Name and Value
Ex. String User_Name=“ Miley Cyrus”
Rules in Using Variables
A variable can contain:
1. Any Unicode character that is a letter(including numeric
letters like Roman numerals) or a digit.
2. Currency sign(such as $)
3. Connecting punctuation character(such as _).
Rules in Using Variables
A variable cannot :
1.Variable names are case-sensitive.
2. A variable ‘s name can be any legal identifier.
3. It can contain unicode letter, digits and two special characters such
as underscore and $ dollar sign.
4. Length of variable name can be any number.
5. It is necessary to use alphabet at the start(however, we can use
underscore).
Rules in Using Variables
A variable cannot :
6. Some auto generated variables may contain ‘$’ sign. But try to
avoid $ sign.
7. White space is not permitted.
8. Special characters are not allowed.
9. A digit at the start is not allowed.
Rules in Using Variables
A variable cannot :
10.Subsequent characters maybe letters, digits, dollar signs, or
underscore characters.
11.Variable name must not be a keyword or reserved word.
Constant Variables
- refers to those variables whose value does not change during
program execution
Ex. final tax=26.7
Importants of Comments on Programs
1. Program documentation contains information that improves
the readability of the program.
2. Comments are part of program documentation because they
help others read and understand how the program works . The
compilers and interpreters however ignore comments and they
are not translated in machine language
Types of Comments in Java
1. Multiple line comments- are comments enclosed by /* and /*
Ex. /** test.java
**Author: Jake Cyrus
**/
2.Single line comments- are comments placed after // symbols.
Ex. // math.java
Identifiers
- are names given to various program elements such as variables,
functions and arrays
The ff. are the guidelines in creating
identifiers
1. It consist of letters and digits without space, in any other ,
except that the first character must be a letter.
2. Uppercase and lowercase are permitted but they are not
interchangeable. As an exception, an underscore may be used
as the first character of the identifier.
3. Reserve words and standard methods must not be used.
Examples of Valid Identifiers
Salary_Per_Month
Age
Grade23
_area_circle
PERSONS_NAME
Y
passing_grade75
Users_Name
Examples of Invalid Identifiers
Invalid Identifiers Reasons of Invalidity
% Remainder of division
|| LOGICAL OR (a | | b ) is true
+= adds right operand to the left operand and a+=b is same as a=a+b
assign the result to left
operand
Assignment Operators
OPERATO DESCRIPTION EXAMPLE
R
operand
/= divides left operand with the right operand a/=b is same as a=a/b
and assign the result to left operand
Carriage return \r
Form feed \f
Escape Characters
backslash \\
Single quote \’
Double quote \”
tab \t
backspace \b