0% found this document useful (0 votes)
11 views

Unit 1 and 2 Java Fundamentals

Java was initially created by Sun Microsystems as Oak to develop platform independent software for consumer devices. It was renamed to Java in 1995 and designed to produce code that could run on various CPUs and environments. Java allows development of both standalone applications and web applets, small programs designed to run in web browsers. The key aspect of Java is that source code is compiled to bytecode that runs on the Java Virtual Machine (JVM), allowing programs to run on any device with a JVM.

Uploaded by

me.harsh12.2.9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit 1 and 2 Java Fundamentals

Java was initially created by Sun Microsystems as Oak to develop platform independent software for consumer devices. It was renamed to Java in 1995 and designed to produce code that could run on various CPUs and environments. Java allows development of both standalone applications and web applets, small programs designed to run in web browsers. The key aspect of Java is that source code is compiled to bytecode that runs on the Java Virtual Machine (JVM), allowing programs to run on any device with a JVM.

Uploaded by

me.harsh12.2.9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

INTRODUCTION

 Java is developed by Sun Microsystems.


 This language was initially called “Oak” but was renamed “Java”
in 1995.

 Java was developed for creating platform independent


software for various consumer electronic devices.
 In an attempt to find such a solution,
 Gosling and others began work on a portable, platform-
independent language that could be used to produce code
that would run on a variety of CPUs under differing
environments. This effort ultimately led to the creation of Java
 This second force was, of course, the World Wide Web.
 Java is also needed for Portable Internet programs.
 Java program are secure and portable
INTRODUCTION
 Java creates two types of programs
 Application Programs
 It is a program that runs on your computer, under
the operating system of that computer. That is,
an application created by Java is more or less like
one created using C or C++.
 Applet
◦ It is a program designed to be transmitted over the
Internet and executed by a Java-compatible Web
browser.
◦ The important difference is that an applet is an
intelligent program, not just an animation or media file.
In other words, an applet is a program that can react to
user input and dynamically change—not just run the
same animation or sound over and over.
Program

 Set of instructions that tell a computer what to do


 To write programs we use programming language
 Machine language: binary language eg.,
 Pros and cons of machine language
 Assembly language: symbolic language
 Machine dependent eg.
 Need a translator called Assembler
Program
High level language: machine independent, instructions
are called statements
Program is generally called a source program or source
code
Result = 2+3
A compiler or interpreter is used to translate the source
code to machine code

Syntax and semantics


Program
API also known as library
Contains predefined java code that we can use to
develop programs

jdk: set of programs that enable us to develop our


programs
It contains JRE that is used o run our programs

JRE and JDK contain JVM


JVM executes our java programs on different machines
IDE
To write source code
Compile the code
Debug – tools to find errors
Run execute program

Eg. NetBeans, Eclipse, BlueJ


 https://medium.com/@rinu.gour123/
introduction-to-java-programming-language-
389033edb91f
OOP Principles
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
Java Environment
 Java environment includes a large number of
development tools and classes and methods.
 The development tools are part of the system

known as java development kit (JDK) and java


standard library (JSL) also known as
application programming interface (API)
Java Development Kit
 Appletviewer
 Javac (compiler)
 Java (interpreter)
 Javap(java disassembler)
 Javah( for c header file)
 Javadoc(for creating HTML document)
 Jdb(java debugger)
 https://medium.com/@rinu.gour123/introduction-to-
java-programming-language-389033edb91f
JVM & Bytecode
 The key that allows Java to solve both the
security and the portability problems just
described is that the output of a Java
compiler is not executable code. Rather, it is
bytecode.
 Java Virtual Machine (JVM) is Java run time
system, which interpret source code to
BYTECODE instead of executable code.
 Bytecode is a highly optimized set of
instructions designed to be executed by the
java run time system (JVM)
Basic program
Class: a blueprint to create objects
Object: an instance of a class

Method: group of instructions to do a specific task


 Main() method is automatically called when we run our

program
 Pascal case: ThisIsName - is used with classes

 Camel case convention: thisIsName– used with methods

 Snake case convention: this_is_name


Basic program structure
 Class Demo{
 public static void main (String[] args)
 {
◦ Body of main
 }
 }

 Save as : Demo.java
 Compile: javac Demo.java
 Run: java Demo
Command line arguments
 When user wants to pass information into a
program
 Whatever is passed to main() is stored as

String array
 class CommandLineExample{
 public static void main(String args[]){
 System.out.println("Your first argument is: "+

args[0]);
 }
 }
Java Basic
 Lexical Issues- White space, Identifiers,
Literals, comments, Separators

 Java Class Libraries

 Data Types- Integers, Floating Point


numbers, Characters, Boolean.

 Variable Declaration
type identifier = value;
Java Keywords
Integer
Floating point

Character type
 Java uses unicode to represent character.
 16 bit. Range is 0 to 65536
 Type Conversion and Casting

 For automatic conversions the two types


should be compatible and destination type
should be larger than the source type.

 Explicit conversion
(target-type) value

 Arrays
type var-name[ ]; or type[ ] var-name;
Scope and lifetime of variable
 Operators – Arithmetic, Bitwise, Relational,
Logical, Assignment.

 ? Operator
exp1 ? exp2 : exp3
Arithmetic
Relational
Control statements
 Selection Statements- if-else, switch case

 Iteration Statements- while, do-while, for.

 Jump Statements- break, continue, return.


Arrays
 Group of like typed variables
 A specific element in an array is accessed by

its index.
 Index starts for 0 to n-1.
 One dimensional, two dimensional, 3

dimensional etc.
 Size of array is found in its length instance

variable.
1D arrays
 Type var-name[];
 Var-name = new type[size];
 E.g. Int num[];
 num = new int[5]; or
 int num= new int[5];
 Arrays can be initialized when they are
declared.
 E.g. int num[] = {1,2,3,4,5};
Multidimensional array
 Int twoD[][] = new int[4][5];
 or
 Int twoD[][]= new int[2][];
 twoD[0]= new int[5];
 twoD[1]= new int[5];
 Or initialize array when declaring
 Int twoD[][]={{1,2},{3,4}};
 Char array is declared as String object
 String str=“This is a test”;
Multi Dimensional Array
Program
Java Scanner
 Java provides various ways to read input from
the keyboard, the java.util.Scanner class is
one of them.
 we can get input from the user in primitive

types such as int, long, double, byte, float,


short, etc.
 Scanner in = new Scanner(System.in);
Java Scanner
 import java.util.*;
 public class ScannerClassExample1 {
 public static void main(String args[]){

 System.out.println("--------Enter Your Details-------- ");


 Scanner in = new Scanner(System.in);
 System.out.print("Enter your name: ");
 String name = in.next();
 System.out.println("Name: " + name);
 System.out.print("Enter your age: ");
 int i = in.nextInt();
 System.out.println("Age: " + i);
 System.out.print("Enter your salary: ");
 double d = in.nextDouble();
 System.out.println("Salary: " + d);
 in.close();
 }
 }

You might also like