24
Fundamentals of Java Programming https://www.article.education

Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

  • Upload
    others

  • View
    25

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Fundamentals of Java Programming

https://www.article.education

Page 2: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Overview

• Exercises (Previous Lesson)

• The JAVA Programming Languages

• Java Virtual Machine

• Characteristics

• What is a class?

• JAVA Standards

• JAVA Keywords

• How Install JAVA

• Sample Java Program

2https://www.article.education

Page 3: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Exercises

Draw a flowchart for the following

1. Enter two number from key bard and print average

2. Enter three number and find the maximum number

3. Process of the ATM machine

3https://www.article.education

Page 4: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

JAVA

• Is a programming language created by James

Gosling from Sun Microsystems in 1991

• Is a general-purpose, class-based, object-

oriented Programming language

• is intended to let application developers

“write once, run anywhere.”

• Current version (JDK) 7u21

• URL : http://www.oracle.com/

https://www.article.education

Page 5: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Java cont…

• Java programming language consists– Java compiler:

Java compiler translates Java coding into

byte-code

– Java virtual machine(JVM)

Java virtual machine interprets this byte-codeand runs the program

– Java class libraries

Java Class Library is a set of dynamicallyloadable libraries that Java applications cancall at run time

5https://www.article.education

Page 6: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Java Virtual Machine (JVM)

Architecture

JAVA Source File(.java)

Java Virtual Machine

Byte code VerifierJava Compiler

MemoryManager

Interpreter

JAVA Bite code(.class) Java API

6https://www.article.education

Page 7: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

JAVA Vs C++

C++ Source File(.cpp)

JAVA Source File(.java)

C++ CompilerJava Compiler

Executable program(.exe)

JAVA Bite code(.class)

Operatingsystem can

directly execute

JAVA VM candirectly execute

Java VM

7https://www.article.education

Page 8: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Characteristics

Java has the following properties

• Platform independent

• Object-orientated programming

language

• Strongly-typed programming language

• Interpreted and compiled language

• Automatic memory management

8https://www.article.education

Page 9: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Platform independent

• Can Run on any Platform

– WindowsJAVA Source File

(.java)

– Linux

– MaC OS Java Compiler

JAVA Bite code(.class)

Java VM

9https://www.article.education

Page 10: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Object-orientated programming

language

• Object-oriented programming (OOP) is aprogramming paradigm that represents conceptsas "objects" that have data fields (State) andmethods (Behavior)

• What is an Objects?– Object is a software bundle of related state

and behavior

– Characteristics: state and behavior

– Example (Person)• State (Name, NIC, height)

• Behavior (Speech, Sleep, eat)

– Object is an instance of a class(instance is a specific realization of any object)

10https://www.article.education

Page 11: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

What is a class?

• A Java class is a group of Java

methods and variables

• Example (Person)

– State (Name, NIC, height)

– Behavior (Speech, Sleep, eat)

class Person

{

}

11https://www.article.education

Page 12: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Requirements for Class name

• Class name must begin with letter of

the alphabet

• Contains only letters, digits,

underscores or dollar sign

• Cannot be a language reserved

keywords (public, class etc)

• Name cannot be following values (true,

false or null)

12https://www.article.education

Page 13: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Java Class name standard

• Begin with uppercase letter

• No spaces

• Emphasizes new word with an initialuppercase letter

Example

• EmployeRecords

• Student

• FirstExample

• SampleProgram

13https://www.article.education

Page 14: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Java Keywords

14https://www.article.education

Page 15: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Install JAVA

• Download Java Development Kit (JDK)

– http://www.oracle.com

• RUN Installation setup

• SET PATH for the JAVA

• Test JAVA is working

• Video Link

– http://www.dscs.sjp.ac.lk/~budditha/java/vd/ins/install.html

15https://www.article.education

Page 16: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Simple Java Program

• A class with a main Methodpublic class FirstProgram{

public static void main(String[] args){

System.out.println("Hello");}

}

FirstProgram

OperatingSystem

OutputMain Method

16https://www.article.education

Page 17: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

A class without a main Methodpublic class FirstProgram{

}

Operating System Can not Execute the program

FirstProgram

OperatingSystem

17https://www.article.education

Page 18: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Your first Java program

• Open a text editor (text pad, Notepad etc.)

• Type the following sample

• Save program as “FirstProgram.java”

public class FirstProgram

{

public static void main(String[] args)

{

System.out.println("Hello World");

}

}

18https://www.article.education

Page 19: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Compile and RUN

• Using command prompt go to the place

where in your java file

• To Compile: type javac <space><fileName>

Javac FirstProgram.java

• To Run: type java<space><fileName>

Java FirstProgram

• Video Link

– http://www.dscs.sjp.ac.lk/~budditha/java/vd/s1/02.html

19https://www.article.education

Page 20: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

First Java program

public class FirstProgram

{

public static void main(String[] args)

{

System.out.println("Hello World");

}

}

Flow chart

Start

Display“Hello World”

End20https://www.article.education

Page 21: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Print Output on Command

window

• Function

– System.out.println(“Some Text”);

– System.out.print(“Some Text”);

• Example

public class FirstProgram{

public static void main(String[] args){

System.out.println("Hello World");}

}21https://www.article.education

Page 22: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Escape Sequences

• A character preceded by a backslash (\) is an

escape sequence

• has special meaning to the compiler

Escape Sequence Description

\t

\b

\n

\r

\f

\'

Insert a tab in the text at this point.

Insert a backspace in the text at this point.

Insert a newline in the text at this point.

Insert a carriage return in the text at this point.

Insert a formfeed in the text at this point.

Insert a single quote character in the text at this point.

\"

\\

Insert a double quote character in the text at this point.

Insert a backslash character in the text at this point.

22https://www.article.education

Page 23: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

ASCII art with JAVA• ASCII art is a graphic design technique that uses computers for

presentation and consists of pictures pieced together from the

95 printable (from a total of 128) characters defined by the

ASCII Standard from

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

System.out.println("

@ @ @

@ @ @ @

@ @ @

\\|/

");

");

");

");

");

");

");

");

");

");

| | |

| | |

| | |

| | |

( )

^^^^^ 23https://www.article.education

Page 24: Fundamentals of Java Programming...JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based, object-oriented Programming

Exercise1. Why JAVA is Platform independent?

2. What class ?

3. What is JAVA Runtime Environment (JRE) ?

4. What is JAVA Class file?

5. Write a JAVA Program to Display the Following

output

-------------------------------

HSIT 2130

Fundamentals of Programming

-------------------------------

6. Try to run the above program on the Linux machine24https://www.article.education