Java Basics
Java Basics
Program?
By
Manjiri Tatke
Objectives
• After completing this lesson, you should be able to:
• Contrast the terms “platform-dependent” and “platform-independent”
• Describe the purpose of the JVM
• Explain the difference between a procedural program and an object-oriented
program
• Describe the purpose of javac and java executables
• Verify the Java version on your system
• Compile and run a Java program from the command line
Topics
• Introduction to computer programs
• Introduction to the Java language
• Verifying the Java development environments
• Running and testing a Java program
Purpose of a Computer Program
• A computer program is a set of instructions that run on a computer or
other digital device.
• At the machine level, the program consists of binary instructions (1s and 0s).
• Machine code
• Most programs are written in high-level code (readable).
• Must be translated to machine code
Translating High-Level Code to
Machine Code
Solaris OS
C Compiler
Solaris OS Binary
Linux
C Compiler
Linux Binary
C Code
Microsoft Windows
C Compiler
Microsoft Windows
Binary
Linked to Platform-Specific Libraries
Project
Libraries
Solaris OS
C Compiler
Project
Libraries
Linux
C Compiler
Linux Binary Linux Executable
Project
Libraries
Microsoft Windows
Microsoft Windows C Compiler Microsoft Windows
Binary Executable
Platform-Dependent Programs
Java Code
Solaris OS
Java
Workstation
Bytecode
(.class file)
JRE
Linux
JRE Workstation
Java Virtual
Machine
(JVM)
Microsoft Windows
Workstation
Procedural Programming Languages
1 Step 1
• Many early programming languages followed a
paradigm called Procedural Programming.
• These languages use a sequential pattern of program execution.
• Drawbacks to procedural programming: 2 Step 2
4 Step 4
5 Step 5
Java Is an Object-Oriented Language
• Interaction of objects
• No prescribed sequence
• Benefits:
• Modularity
• Information hiding
• Code reuse
• Maintainability
What is Java?
• Java is an Object-Oriented programming language – most of it is free and open
source!
• It is developed in the early 1990s, by James Gosling of Sun Microsystems
• It allows development of software applications.
• It is amongst the preferred choice for developing internet-based applications
Topics
PATH
points
here
Runtime
Compiler
Topics
• Syntax:
javac SayHello.java
Executing (Testing) a Program
1.Go to the directory where the class files are stored.
2.Enter the following for the class file that contains the main method:
• Syntax:
java <classname>
• Example:
Do not specify .class.
java SayHello
• Output:
Hello World!
Output for a Java Program
• A Java program can output data in many ways. Here are some
examples:
• To a file or database
• To the console
• To a webpage or other user interface
Platform Independence feature of
Java
Unix on Pentium System
Class file
containing Windows Pentium PC system
Bytecodes
Class Loader
Bytecode Verifier Pentium machine
Class Loader JIT compiler level instructions
Bytecode verifier
JIT compiler
JDK 11: Launch Single-File Source-Code
Programs
Benefits: Circle.java
method. }
Use Cases:
java Circle.java
• Experiment quickly to learn Java.
Area of circle=176.714…
• Write small utility or "shebang" files.
Java Language Features
• Java has advantages due to the following features:
• Completely Object-Oriented
• Simple
• Robust: Strongly typed language
• Security
• Byte code Verifier
• Class Loader
• Security Manager
• Architecture Neutral: Platform independent
• Interpreted and Compiled
• Multithreaded: Concurrent running tasks
• Dynamic
• Memory Management and Garbage Collection
Quiz
• Which of the following is correct? (Choose all that apply.)
a. javac OrderClass
b. java OrderClass
c. javac OrderClass.java
d. java OrderClass.java
Summary
• In this lesson, you should have learned how to:
• Describe the distinction between high-level language and machine code
• Describe what platform-independence means
• Describe how a Java program is compiled and to what format
• Explain what it means to say that Java is an object-oriented language
• Determine the version number of a Java install
• Compile and run a Java program from the command line