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

Unit-I 1JavaBAsic Intro

Uploaded by

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

Unit-I 1JavaBAsic Intro

Uploaded by

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

JAVA

PROGRAMMI
NG
NEED OF PROGRAMMING LANGUAGE
INTRODUCTION

• Java is an object-oriented simple programming language.


• It is a general-purpose, high-level programming language that helps programmers and developers to
write a code once and run it anywhere.
• Java is considered both a compiled and interpreted language.
• platform-independent programming language.
• Java is a programming language that James Gosling developed at Sun Microsystems_Inc in the year 1995,
which later was taken into possession by the Oracle Corporation in 2009.
HISTORY
• started by a team led by James Gosling in 1991. The team was named as Green-

Team. (James Gosling, Mike Sheridan, and Patrick Naughton)

• started by extending the C++ which was currently going on at that time.

• Initially it was designed for small, embedded systems in electronic appliances like

set-top boxes.

• Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.

• After that, it was called Oak and was developed as a part of the Green project.

• Java is an island in 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 a cup of coffee nearby his office.


JAVA VERSIONS
Java Versions Release Date Java SE 10 20th Mar 2018

JDK Alpha and Beta 1995 JAVA SE 11 25th Sep 2018

JDK 1.0 23rd Jan 1996 JAVA SE 12 19th Mar 2019

JDK 1.1 19th Feb 1997 JAVA SE 13 17th Sep 2019

J2SE 1.2 8th Dec 1998 JAVA SE 14 17th Mar 2020

J2SE 1.3 8th May 2000 JAVA SE 15 15th Sep 2020

J2SE 1.4 6th Feb 2002 Java SE 17 14th Sep 2021

J2SE 5.0 30th Sep 2004 Java SE 18 March 22, 2022.

Java SE 6 11th Dec 2006 Java SE 19 17th Jan,2023 (latest version)

Java SE 7 28th July 2011

Java SE 8 18th Mar 2014

Java SE 9 21st Sep 2017


Simula

JavaScript PHP

Java
OOP R-

Languages
programming

Kotlin Ruby

C++
JAVA FEATURES (BUZZWORDS)
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
SIMPLE
• Java is very easy to learn, and
• Its syntax is simple, clean and easy to understand.
• According to Sun Microsystem, Java language is a simple programming language because:
• Java syntax is based on C++
• Java has removed many complicated and rarely-used features.
• Explicit pointers, operator overloading, etc.
• There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.
OBJECT-ORIENTED
• Java is an object-oriented programming language.
• Everything in Java is an object.
• Object-oriented means we organize software as a combination of different types of
objects that incorporate both data and behavior.
• Basic concepts of OOPs are object, class, inheritance, polymorphism, abstraction,
encapsulation.
PLATFORM INDEPENDENT
• it is different from other languages like C, C++, etc. which are compiled into platform specific
machines .
• Java is guaranteed to be write-once, run-anywhere language.
• On compilation Java program is compiled into bytecode. This bytecode is platform independent and
can be run on any machine, plus this bytecode format also provide security. Any machine with Java
Runtime Environment can run Java Programs.
PORTABLE
• Java is portable because it facilitates you to carry the Java
bytecode to any platform.
• It doesn't require any implementation.
• Everything related to storage is predefined, example: size of
primitive data types
• The WORA (Write Once Run Anywhere) concept and
platform-independent feature make Java portable
ROBUST
• Java is robust because: it is capable of handling unexpected termination of a
program.
• Exception handling and the type checking mechanism in Java.

• It uses strong memory management.

• There is a lack of pointers that avoids security problems.

• Java provides automatic garbage collection which runs on the JVM.

• All these points make Java robust.


SECURED
• Java is best known for its security.
• With Java, we can develop virus-free systems.- If a virus is
found in a bytecode file, JVM will throw an exception and
abort execution.
• Java Programs run inside a virtual machine sandbox.
•Classloader: part of the (JRE) which is used to load Java classes into the Java Virtual
Machine dynamically. It adds security by separating the package for the classes of the
local file system from those that are imported from network sources.
•Bytecode Verifier: It checks the code fragments for illegal code that can violate
access rights to objects.
•Security Manager: It determines what resources a class can access such as reading
and writing to the local disk.
ARCHITECTURE-NEUTRAL
• Java is architecture neutral because there are no implementation dependent
features.
• E.g. The size of primitive types is fixed.
• In C programming,
• int data type occupies

• 2 bytes of memory for 32-bit architecture and


• 4 bytes of memory for 64-bit architecture.

• Int occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
DISTRIBUTED
• Java is also a distributed language.
• Programs can be designed to run on computer networks.
• Java has a special class library for communicating using TCP/IP protocols.
• Creating network connections is very much easy in Java as compared to C/C++.
INTERPRETED

• The interpreter converts the source code line-by-line during the RUN Time.
• responsible for reading and executing the program.
• Java programming language uses both a compiler and an interpreter.
• Java programs are compiled to generate bytecode files then JVM interprets the bytecode file during
execution. Along with this JVM also uses a JIT compiler (it increases the speed of execution).
MULTI-THREADED
• Java multithreading feature makes it possible to write program that can do many tasks
simultaneously.

• Benefit of multithreading is that it utilizes same memory and other resources to execute
multiple threads at the same time, like While typing, grammatical errors are checked along.

• Java supports “Multitasking”. Multitasking is when multiple jobs are executed


simultaneously. Multitasking improves CPU and Main Memory Utilization .
DYNAMIC

• Java is a dynamic language.


• It supports the dynamic loading of classes.
• It means classes are loaded on demand.
• It also supports functions from its native languages, i.e., C and C++.
• Java supports dynamic compilation and automatic memory management (garbage
collection).
Java Platforms / Editions
• Java Platform is a collection of programs that help programmers to develop and run Java
programming applications efficiently.
• It includes an execution engine, a compiler, and a set of libraries in it.
• It is a set of computer software and specifications.
• There are 4 platforms or editions of Java:

1) Java SE (Java Standard Edition)

2) Java EE (Java Enterprise Edition)

3) Java ME (Java Micro Edition)

4) JavaFX

19
Java Platforms / Editions

1) Java SE (Java Standard Edition)

• It is a Java programming platform.


• It includes Java programming APIs
• java.lang, java.io, java.net, java.util, java.sql, java.math etc.
• It includes core topics like OOPs, String, Exception, Inner classes, Multithreading,
I/O Stream, Networking, AWT, Swing, Collection, etc.

20
Java Platforms / Editions
2) Java EE (Java Enterprise Edition)

• It is an enterprise platform that is mainly used to develop web and


enterprise applications.
• It is built on top of the Java SE platform.
• It includes topics like Servlet, JSP, Web Services, EJB etc.

3) Java ME (Java Micro Edition)

• It is a micro platform that is dedicated to mobile applications.

4) JavaFX

• It is used to develop rich internet applications. It uses a lightweight user


interface API. 21
JAVA PROGRAMMING ENVIRONMENT
• Java Development Kit: (JDK)
• It is a software development environment used to develop Java applications and applets.
• It contains JRE and several development tools, an interpreter/loader (java), a compiler
(javac), an archiver (jar), a documentation generator (javadoc) accompanied with another
tool.

• Java Runtime Environment:


• The JRE software builds a runtime environment in which Java programs can be executed.
• The JRE is the on-disk system that takes your Java code, combines it with the needed
libraries, and starts the JVM to execute it.
• The JRE contains libraries and software needed by your Java programs to run.
• JRE is a part of JDK (which we will study later) but can be downloaded separately.
• Java Virtual Machine:
• Java applications are called WORA because of their ability to run a code on any
platform. This is done only because of JVM. The JVM is a Java platform component that
provides an environment for executing Java programs. JVM interprets the bytecode into
machine code which is executed in the machine in which the Java program runs.
• JVM performs the following functions:
• Loads the code
• Verifies the code
• Executes the code
• Provides a run-time environment for various applications.
• JVM provides a Memory area.
ARCHITECTURE OF JAVA VIRTUAL
MACHINE (JVM)
HOW TO INSTALL JAVA
JAVA PROGRAM STRUCTURE
A SIMPLE JAVA PROGRAM
• A multiline comment.
• This type of comment must begin with /* and end with */.
• A single-line comment, shown here: // Line1
• The keyword class to declare that a new class is being defined.
• Simple is an identifier that is the name of the class.
• Class definition, including all of its members, between the opening curly brace
({) and the closing curly brace (}).
A SIMPLE JAVA PROGRAM
• public static void main(String args[])
• This is the main( ) method
• All Java applications begin execution by calling main( ).

• The public keyword is an access modifier.


• It allows the programmer to control the visibility of class members.
• Member may be accessed by code outside the class in which it is declared.
• Making the main() method public makes it globally available.

• The keyword static allows main( ) to be called without having to instantiate a


particular instance of the class.
• The main() method is static so that JVM can invoke it without instantiating the
class.
• The keyword void
• It simply tells the compiler that main( ) does not return a value.
• Void is used to specify that a method doesn’t return anything.

• String args[ ]
• It declares a parameter named args, which is an array of instances of the class String.
• It stores Java command line arguments and is an array of type java.lang.String class.

• Output is actually accomplished by the built-in println( ) method.


A Simple Java Program- Steps
• Java program  Source file  Simple.java

• Compile (Javac) Class file  Simple.class

• Interpret (JIT/JVM)  Machine/object code  Simple.obj

• Execute (JRE)

• Java file compilation  javac Simple.java


• Execution  java Simple
34
DATA TYPES
PRIMITIVE DATA TYPES
• primitive data types specify the size and type of variable values. They are the building
blocks of data manipulation and cannot be further divided into simpler data types.
• There are 8 types of Primitive data types in Java – Boolean, char, byte, int, short, long,
float, and double.
Boolean type – Boolean
 A boolean data type can store either True or False.
 They can be used to check whether two values are equal or not (basically in
conditional statements to return True or False).
 Typically, programmers use it as a flag variable to track true or false conditions.
 The default Boolean value is False..
PRIMITIVE DATA TYPES CONTINUE……..
• Character type – char
 The char data type stores a single character.
 It stores lowercase and uppercase characters, which must be enclosed in single quotes.
 The char data type in Java supports Unicode characters and provides provision to multiple
languages like English, French, German, etc.
 It takes memory space of 16 bits or 2 bytes.
 The values stored range between 0 to 65536. class CharDataType {
public static void main(String[] args) {
char var1 = 'A';
char var2 = 'd';
System.out.println(var1);
System.out.println(var2);
}
}
• Integer type –
• An integer type stores an integer number with no fractional or decimal places. Java has four integer
types – byte, short, int, and long.
• Byte
• The byte is the smallest data type among all the integer data types. It is an 8-bit signed two’s complement
integer. It stores whole numbers ranging from -128 to 127.
• Syntax:
• byte byteVariable;

• Short
• Short is a 16-bit signed two’s complement integer. It stores whole numbers with values ranging from -
32768 to 32767. Its default value is 0.
• Syntax:
• short shortVariable;
• Int
• Int is a 32-bit signed two’s complement integer that stores integral values ranging from 2147483648 (-
2^31) to 2147483647 (2^31 -1). Its default value is 0.
• Syntax:
• int intVariable;
• Long
• long is a 64-bit signed two’s complement integer that stores values ranging from -
9223372036854775808(-2^63) to 9223372036854775807(2^63 -1). It is used when we need a range of
values more than those provided by int. Its default value is 0L. This data type ends with ‘L’ or ‘l’.
• Syntax:
• long longVariable;
VARIABLES
• A variable is a memory location or storage area to hold the data.
• Each variable should be given a unique Name (Identifier) to indicate the Storage Area.
JAVA COMMAND LINE ARGUMENTS

• used to pass arguments to the main program.


• argument is passed at the time of running the java program.
• The arguments passed from the console can be received in the java program and it can be used as an
input.
• You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

1.compile by > javac CommandLineExample.java


2.run by > java CommandLineExample Welcome
EXAMPLE

class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
INPUT / OUTPUT FUNCTIONS IN JAVA
READING INPUT FROM CONSOLE

• Input can be given either from file or keyword. In java, input can be read from console in 3 ways:
• BufferedReader :- use the class “BufferedReader” and create the object “bufferedreader”. We
also take integer value and fetch string from the user.
• Scanner: It accepts multiple inputs from file or keyboard and divides into tokens. It has methods to
different types of input (int, float, string, long, double, byte) where tokenizer does not have.
SCANNER CLASS
• this class belongs to the java.util package.
• import java.util.Scanner;
• create an object of the Scanner class in java, you need to pass System.in in the constructor of the
Scanner class. System is a class in Java and in is a static variable of type InputStream . System.in
represents the standard input stream.
• Scanner input = new Scanner(System.in);
FUNCTION DESCRIPTION

nextInt() It is used to read an int value from the user

nextBoolean() It is used to read a boolean value from the user

nextFloat() It is used to read a float value from the user

nextDouble() It is used to read a double value from the user

nextLine() It is used to read a String value from the user

nextLong() It is used to read a long value from the user

nextShort() It is used to read a short value from the user

nextByte() It is used to read a byte value from the user


PROGRAMS

• Program to check whether the number is positive or negative.


• Program to find day of week using switch statement.
• Program To find the average age of a group of people using a for loop.

You might also like