Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Fundamentals

Uploaded by

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

Java Fundamentals

Uploaded by

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

Module 3: 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.

1. James Gosling, Mike Sheridan, and Patrick Naughton


initiated the Java language project in June 1991. The small
team of sun engineers called Green Team.
2. Initially designed for small, embedded systems in electronic
appliances like set-top boxes.
3. Firstly, it was called "Greentalk" by James Gosling, and the
file extension was ‘.gt’.
4. After that, it was called Oak and was developed as a part of
the Green project.
Why Java named "Oak"?

5. Why Oak? Oak is a symbol of strength and chosen


as a national tree of many countries like the U.S.A.,
France, Germany, Romania, etc.

6. In 1995, Oak was renamed as "Java" because it was


already a trademark by Oak Technologies.
Why Java Programming named "Java"?
7. Why had they chosen java name for Java language? The team
gathered to choose a new name. The suggested words were
"dynamic", "revolutionary", "Silk", "jolt", "DNA", etc. They wanted
something that reflected the essence of the technology:
revolutionary, dynamic, lively, cool, unique, and easy to spell and fun
to say. According to James Gosling, "Java was one of the top
choices along with Silk". Since Java was so unique, most of the
team members preferred Java than other names.
8. Java is an island of Indonesia where the first coffee was produced
(called java coffee). It is a kind of espresso bean. Java name was
chosen by James Gosling while having coffee near his office.
9. Notice that Java is just a name, not an acronym
Why Java Programming named "Java"?
10. Initially developed by James Gosling at Sun
Microsystems (which is now a subsidiary of Oracle
Corporation) and released in 1995.
11. In 1995, Time magazine called Java one of the Ten
Best Products of 1995.

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.

main() method println() method


Any code inside Inside the main() method, we can use the println()
the main() method method to print a line of text to the screen.
will be executed.

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

Stores whole numbers from -2,147,483,648 to


int 4 bytes
2,147,483,647

Stores whole numbers from -9,223,372,036,854,775,808 to


long 8 bytes
9,223,372,036,854,775,807

Stores fractional numbers. Sufficient for storing 6 to 7


float 4 bytes
decimal digits

Stores fractional numbers. Sufficient for storing 15 decimal


double 8 bytes
digits

boolean 1 bit Stores true or false values


char 2 bytes Stores a single character/letter or ASCII values
String Stores a sequence of character (text)
2 types of data (Primitive and Non-Primitive)
• Primitive types are predefined (already defined) in Java. Non-
primitive types are created by the programmer and is not defined by
Java (except for String).
• Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
• A primitive type always has a value, while non-primitive types can be
null.
• A primitive type starts with a lowercase letter, while non-primitive
types starts with an uppercase letter.
• Non-primitive data types are called reference types because they
refer to objects.
• Examples of non-primitive types are Strings, Arrays, Classes,
Interface, etc.
Declaring (creating) variables
Print variables
Identifiers
• All Java variables must be identified with unique names.
• These unique names are called identifiers.
• Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
• Note: It is recommended to use descriptive names in order to create understandable
and maintainable code:
The general rules for naming variables are:

• Names can contain letters, digits, underscores, and dollar


signs
• Names must begin with a letter
• Names should start with a lowercase letter, and cannot
contain whitespace
• Names can also begin with $ and _
• Names are case-sensitive ("myVar" and "myvar" are different
variables)
• Reserved words (like Java keywords, such as int or boolean)
cannot be used as names
Seatwork 1:
Create a Java Program that will display your :
1. Student ID number
2. Full name
3. Program course & Section
4. Age
5. Weight and Height
6. Address

Note: You must use java variables & data types.


Types of Variables
1. Local Variables
Local Variables are a variable that are declared inside the body of a method.
2. Instance
Variables
Instance variables
are defined
without the
STATIC keyword.
They are defined
Outside a method
declaration. They
are Object specific
and are known as
instance variables.
3. Static Variables
Static variables are initialized only once, at the start of the program execution. These variables should be
initialized first, before the initialization of any instance variables.
Java Type Casting
Java Type Casting - assigning a value of one primitive data type to another type.

Widening Casting (automatically) -


converting a smaller type to a larger
type size
byte -> short -> char -> int -> long ->
float -> double

Narrowing Casting (manually) -


converting a larger type to a smaller
size type
double -> float -> long -> int -> char
-> short -> byte
Real-Life Example of Type Casting
Create a program to calculate the percentage of a user's score in
relation to the maximum score in a game.

• Set the maximum possible score in the game to 500.


• Set the actual score of the user to 423
• Calculate the percent of the user’s score in relation to the
maximum available score.
• Convert user score to float to make sure that the division is
accurate.
Java Arithmetic Operators
Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two value x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increase the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x


Java Assignment Operators
Java Comparison Operators
Java Logical Operators
String Length
A String in Java is actually an object, which contain methods that can perform certain operations on
strings. For example, the length of a string can be found with the length() method:
toUpperCase() & toLowerCase()
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation:
String concat
String Special Character
Because strings must be written within quotes, Java will misunderstand this string, and generate an
error:

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.

You might also like