Java Fundamentals
Java Fundamentals
Objective:
Students will know the fundamental features of an object-oriented language
like Java: Its various features and advantages over other programming
languages; Getting started programming in Java; Terms of software and
background, as well as some basic terminology; Coding the first Java
program.
What is Java?
• Java is an object-oriented programming
language developed by Sun Microsystems, a
company best known for its high-end Unix
workstations. Modeled after C++, the Java
language was designed to be small, simple,
and portable across platforms and operating
systems, both at the source and at the binary
level.
• Java is often mentioned in the same breath as
HotJava, a World Wide Web browser from Sun
like Netscape or Mosaic.
• What makes HotJava different from most other browsers is that, in addition to
all its basic Web features, it can also download and play applets on the
reader’s system.
• Applets appear in a Web page much in the same way
as images do, but unlike images, applets are dynamic
and interactive.
• Applets can be used to create animations, figures, or
areas that can respond to input from the reader,
games, or other interactive effects on the same Web
pages among the text and graphics. Although HotJava
was the first World Wide Web browser to be able to
play Java applets, Java support is rapidly becoming
available in other browsers. Netscape 2.0 provides
support for Java applets, and other browser developers
have also announced support for Java in forthcoming
products.
Java History
• Java was originally designed for interactive television, but it was too
advanced technology for the digital cable television industry at the time.
• The history of Java starts with the Green Team.
• Java team members (also known as Green Team), initiated this project to
develop a language for digital devices such as set-top boxes, televisions,
etc. However, it was suited for internet programming. Later, Java
technology was incorporated by Netscape.
• The principles for creating Java programming were "Simple, Robust,
Portable, Platform-independent, Secured, High Performance,
Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and
Dynamic".
• Java was developed by James Gosling, who is known as the father of
Java, in 1995. James Gosling and his team members started the project
in the early '90s.
Currently, Java is used in internet programming, mobile devices, games, e-business solutions, etc.
There are given significant points that describe the history of Java.
12. JDK 1.0 released in (January 23, 1996). After the first
release of Java, there have been many additional features
added to the language. Now Java is being used in
Windows applications, Web applications, enterprise
applications, mobile applications, cards, etc. Each new
version adds the new features in Java.
Java
Version
History
Java Environment
There are few things which must be clear before setting up the
environment
1. JDK(Java Development Kit) : JDK is intended for software
developers and includes development tools such as the Java
compiler, Javadoc, Jar, and a debugger.
2. JRE(Java Runtime Environment) : JRE contains the parts of the
Java libraries required to run Java programs and is intended for end
users. JRE can be view as a subset of JDK.
3. JVM: JVM (Java Virtual Machine) is an abstract machine. It is
a specification that provides runtime environment in which java
bytecode can be executed. JVMs are available for many hardware
and software platforms.
Module 4: Java Basic Program
How to create a Java Program in Eclipse
IDE?
Project Name: myFirstProgram
Name: myFirstJava
How to run a Java
Program in Eclipse?
1.Go to Run.
2.Click Run or Ctrl + F11
Code
Output
Class
Every line of code that
runs in Java must be
inside a class
Note:
• Java is case-sensitive:
"MyClass" and
"myclass" has different
meaning.
• The name of the java
file must match the
class name.
Note:
• The curly braces {} marks the beginning and the end of a block of code.
• System is a built-in Java class that contains useful members, such as out, which is short for "output". The
println() method, short for "print line", is used to print a value to the screen (or a file).
• You should also note that each code statement must end with a semicolon (;).
Note: When you are working with text, it must be wrapped inside double quotations marks "".
Print / Display Numbers in Java
Comment in Java
Java Variables & Data types
Data Type Size Description
byte 1 byte Stores whole number from -128 to 127
short 2 bytes Stores whole number from -32,768 to 32,767
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\) escape character turns special characters into string characters:
String Special Character
Other common escape sequences that are valid in Java are:
Class & Object in Java
Class Syntax:
class <class_name>{
field;
method;
}
Object Syntax:
ClassName ReferenceVariable = new ClassName();
Java Object & Class: Main method outside class
Java Exceptions – Try..Catch
• When executing Java code, different errors can occur: coding errors made by the programmer, errors
due to wrong input, or other unforeseeable things.
• When an error occurs, Java will normally stop and generate an error message. The technical term for
this is: Java will throw an exception (throw an error).
• The try statement allows you to define a block of code to be tested for errors while it is being executed.
• The catch statement allows you to define a block of code to be executed, if an error occurs in the try
block.
• The try and catch keywords come in pairs:
Syntax:
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
Scanner method
The Scanner class can be used to obtain data from the keyboard, files and strings.