Java
Topics (Unit –I)
1. Differentiate
between C, C++ & Java
2. Java
Introduction (History of Java)
3. Java
Features
4. Java
Applications
5.
Java and Internet
6.
Java and WWW (World Wide
Web)
7.
Java Platform (JDK (JVM+JRE) + API)
8.
Structure of Java Program
9.
Java Character set
10.
Java Escape sequences
11.
Java Tokens
12.
Java Datatypes
13.
Java Input and Output statements
14.
Java Statements
15.
Java Sample Programs
16.
1) Differentiate between C, C++ & Java
|
C |
C++ |
JAVA |
1.
Developed by |
Dennis Ritchie |
Bjarne Stroustrup |
James Gosling |
2.
Year |
1972 |
1980 |
1990(Oak), 1995(Renamed java) |
3.
First Version |
1972 |
1985 |
JDK Alpha & Beta (1995), JDK1.0-1996 |
4.
Software |
Turboc |
Turboc++ |
JDK |
5.
Lab(Company) |
AT&T Bell, USA |
AT&T Bell, USA |
Sun
Micro Systems Adopted Oracle Corp, USA-2010 |
6.
Extension |
.c |
.cpp |
.java |
7.
Derived from |
BCPL |
C |
C(Syntax) & C++ |
8.
Category (Paradigms) |
Structured Oriented Language |
OOP's(Object Oriented Language) |
OOP's(Object
Oriented Language)
|
9.
OOP's Concept |
Not supported |
Supported |
Supported |
10.
Types |
Middle level |
High
level language |
High level language |
11.
Compilation |
Compiler |
Compiler |
Compiler
& Interpreter |
12.Code
Translation |
Compiled |
Compiled |
Interpreted |
13.
Approach |
Top to Bottom |
Bottom to Top |
Bottom to Top |
14. Platform Dependency |
Dependent(WOCA) Write Once Compile Anywhere |
Dependent(WOCA) Write Once Compile Anywhere |
Independent
(WORA) Write Once Runs
Anywhere |
15.
Char Format |
ASCII |
ASCII |
Unicode(UTF) |
16.
Keywords |
32 |
63 |
53 |
17.
Pointer |
Supported |
Supported |
Not supported |
18.
Format Specifier |
need(%d, %c, %f) |
not need |
not need |
19.
goto |
Supported |
Supported |
Not supported |
20.Storage Allocation |
malloc, calloc |
new, delete |
garbage collector |
21.
Header file |
Supported |
Supported |
Not supported (use packages) |
22.Constructor |
Not Supported |
Supported |
Supported |
23.Destructor |
Not Supported |
Supported |
Not Supported |
24.Operator Overloading |
Not Supported |
Supported |
Not Supported |
25.Function Overloading |
Not Supported |
Supported |
Supported |
26.
inline |
Not Supported |
Supported |
Not Supported |
27.
this |
Not Supported |
Supported |
Supported |
28. Multiple
inheritance |
Not Supported |
Supported |
Not Supported |
29. virtual |
Not Supported |
Supported |
Not Supported |
30. :: (Scope Res
olution Operator) |
Not Supported |
Supported |
Not Supported |
31. friend keyword |
Not Supported |
Supported |
Not Supported |
32. interface |
Not Supported |
Not Supported |
Supported |
33. Multithreading |
Not Supported |
Not Supported |
Supported |
35. Exception Handling |
Not Supported |
Supported |
Supported |
36. Database
Connectivity |
Not Supported |
Not Supported |
Supported |
37. Networking
Programming |
Not Supported |
Not Supported |
Supported |
38. Web & Mobile Window(Desktop)
Applications |
Not Supported |
Not Supported |
Supported |
39. Jagged Array |
Not Supported |
Not Supported |
Supported |
40. Constant |
const |
const |
final |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2) Java Introduction (History of Java)
* Java Designed: James Gosling
* Java Developer : Sun Microsystems ,USA
* Original Name : Greentalk After that Oak ,1990’s
* Renamed Java :
Java,1995’s
* Java Team :
Green Project
* First Released : JDK
Alpha & Beta(1995)
* Java Acquired : Oracle Corporation (2010)
* Java License :
GNU General Public License
* Encoding Type : Unicode character set (UTF-16)
* Java
Browser : Hot Java
* Download Type : Open Source Software(OSS)
* Download & Use : Free(no cost), Free download & use
* Environment :
Cross Platform(use Different OS), Machine Independent code(WORA)
* Language Type: OOP’s (High Level Language)
* Compilation :
Bottom To Top Approach
* Java Versions : JDK
Alpha & Beta (1995), JDK 1.0 (1996),
JDK 1.1 (1997), J2SE 1.2 (1998), J2SE 1.3 (2000),J2SE 1.4 (2002),J2SE 5.0
(2004),Java SE 6 (2006),Java SE 7 (2011),
…Java SE 15
(2020)... JDK 23.01 (2024)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3) Java Features
* Simple & Easy to learn
* Case sensitive Language
* Cross Platform & Open Source
Software(OSS)
* Object-Oriented Language
* Portable Language
* Platform (Machine) Independent Language
(WORA-Write Once Runs Anywhere)
* Secured & Robust, Architecture
neutral & Interpreted
* High Performance & Multithreaded
Language
* Distributed (Networking) & Dynamic
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4) Java Applications
* Consumer Electronic Devices (Ex: TV,
VCD, Mobile)
* Mobile Applications
* Embedded Systems & Cloud based
applications
* Web and Web Server Applications
* Desktop or Windows Applications(GUI)
* Business & Enterprise Applications
* Scientific Applications & Big Data
Technologies
* 2D & 3D, Virtual Reality Games(VR)
* Editors & Database Applications
* Robotics (Artificial Intelligence(AI))
* Acrobat reader, media player, antivirus,
Ultra Mixer, Blu-ray BD-J
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5) Java and Internet
Java
and Internet
---------------------
·
Java is strongly associated with the Internet because the first
application program written in Java was HotJava, a browser to run the applet on
Internet. So the Internet users use
the Java to create the applet programs and run them locally using a
'Java-enabled browsers' like HotJava
·
-Interconnected
Network (a connection of all computers to a central network)
·
-Internet is a
network of network
·
-Information Super
Highway, to access information over the web
·
-Internet uses the
standard Internet Protocol (TCP/IP).
·
-Every computer in
internet is identified by a unique IP address.
·
-IP Address is a
unique set of numbers (such as 110.22.33.114) which identifies a computer
location. A special computer DNS (Domain Name Server) is used to give name to
the IP Address. so that user can locate a computer by a name.
6) Java and WWW
·
World Wide Web (WWW), byname the Web, the
leading information retrieval service of the Internet (the worldwide computer
network).
·
The World Wide Web (WWW) is a network of
online content that is formatted in HTML and accessed via HTTP.
·
Java communicates with a Web page through
a special tag called <APPLET>.the Figure
illustrates the process. It shows the following communication steps:
1) The
user sends a request for an HTML document to the remote computer’s Web server. The Web server is a
program that accepts a request, processes the request, and sends the request
document.
2) The
HTML document is returned to the user’s browser. The document contains the
APPLET tag, which identifies the applet.
3) The
corresponding applet byte code is transferred to the user’s computer. This
bytecode had been previously created by the Java compiler using Java source code file
for that applet.
4) The
Java enabled browser on the user’s computer interprets the byte codes and
provides output.
5) The
user may have further interaction with the applet but with no further
downloading from the provider’s Web server. This is because the bytecode
contains all the information necessary to interpret the applet.
Web Browser
-----------------
·
web Browser is an application software
that allows us to view and explore
·
information on the web.
·
User can request for any web page by just
entering a URL into address bar.
·
-Web browser can show text, audio, video,
animation and etc.,
Browser Vendor
----------- ----------
1. Internet
Explorer - Microsoft
2. Google Chrome -
Google
3. Mozilla Firefox
- Mozilla
4. Netscape
Navigator - Netscape Communications Corp.
5. Opera - Opera
Software
6. Safari - Apple
7. Sea Monkey -
Mozilla Foundation
8. K-meleon -
K-meleon
Web Server
-----------------
·
Web server is a computer where the web
content is stored.
·
All Information stored on web server
·
Web server that Store, process &
deliver (Serves up or Response) Web pages to client
·
The communication between client and
server takes place using the Hypertext Transfer Protocol (HTTP).
Ex:
1. Apache - Apache (49%)
2. nginx - NGINX, inc (39%)
3. IIS - Microsoft (9.5%)
4. LiteSpeed – LiteSpeed Technologies(3.4%)
5.
GWS - Google(1.0%)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7) Java Platform (JDK (JVM+JRE) + API)
1.
JDK (Java Development Kit)
----------
- JDK is a software development
Environment.
- It is used for Compile, Debug, Run and
developing java applications and applets.
- It includes the Java Runtime Environment
(JRE) + Developing Tools
List
of Tools (Developing Tools)
1. javac
- Java Compiler
2. java
- Java Interpreter
3. jar
- java archiver
4. rmic
- RMI Compiler
5. javah
- Java Header File generator
6. appletviewer – Run applet programs
7. javadoc - documentation generator
---------------------------------------------------
2.
JVM (Java Virtual Machine)
----------
- JVM is a virtual machine that enables
the execution of Java bytecode
- It Performs Load, Verify, Execute Code
and provide Runtime Environment(JRE).
- JVM is Platform Dependent
Source
CodeàJava
CompileràClass
file (byte code)à Java InterpreteràOS
(.java) (javac) (.class) (JVM) ( Linux, Windows)
----------------------------------------------------
3.
JRE (Java Runtime Environment)
-----
- The Java Runtime Environment is a set of
software tools which are used for developing Java applications.
- It is used to provide the runtime
environment.
- It is the implementation of JVM. It
physically exists.
- It contains a set of libraries + other
files that JVM uses at runtime.
---------------------------------------------------
4.
API (Application Programming Interface)
-----
- API is a Package,
- A java package is a group of similar
types of classes, interfaces and sub-packages.
- There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util, sql etc.
import
package
Ex:
1)
import java.lang.*;
2)
import java.io.*;
3)
import java.util.*;
4)
import java.awt.*;
5)
import java.net.*;
6)
import java.sql.*;
7)
import java.swing.*;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8) Structure of Java Program
Note:
1.
class:
class keyword is used to declare a class in java.
2.
public:
public keyword is an access modifier which represents visibility. It means
it is visible to all.
3.
void:
void is the return type of the method. It means it doesn't return any
value.
4.
static:
static is a keyword. If we declare any method as static, it is known as
the static method. The core advantage of the static method is that there is no
need to create an object to invoke the static method. The main method is
executed by the JVM, so it doesn't require to create an object to invoke the
main method. So it saves memory.
5.
main():
Java program processing starts with the method main(). Each java program must
have only one main() method.
6.
String[] args is
used for command line argument.
//Example
Program for java
class first{
public static void main(String args[]){
System.out.println(“Welcome to java”);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9) Java Character Set
In
Java, A character set defines the
valid characters that can be used in source programs or interpreted
when a program is running.
1.
Alphabets :
All capital (A-Z) and small (a-z) alphabets.
2.
Digits :
All digits 0-9.
3.
Special Symbols: Java supports all kind of special symbols
like, ” ‘ l ; : ! ~ @ # $ % ^ ` & * ( ) _ + – = { } [ ] \ .
4.
White Spaces: White spaces like tab space, blank space,
newline, and carriage return.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10) Java Escape Sequences
-Escape Sequences(characters) is used to perform some specific task
1. \t - insert a tab.
2. \’ - insert a single quote.
3. \" - insert a double quote.
4. \r - insert a carriage return.
5. \\ - insert a backslash character.
6. \n - insert a new line.
7. \f - insert a form feed.
8. \b - insert a backspace.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11) Java Tokens
- The smallest element (Individual units)
of the Java program is called tokens.
There are six types of Tokens
------------------------------------
1.
Identifier
2.
Keywords
3.
Constant(Literals)
4.
Operators
5.
Separators
6.
Comment
1.
Identifier:
---------------
- Identifiers are the names given to the fundamental
building blocks in a program.
- Identifier can be a name of the
variables, class, object, functions, arrays
Variable
-----------
·
Variable is used to store a value during a
runtime
·
Variable also known as identifier. Variable
is a name of memory location (Reserved Area).
·
Variables are case sensitive (A and a both
are two different variables).
Declare
a java variable:
Syntax:
datatype variable = value;
Example:
int a=10;
float b=13.5;
String name="jahab";
There are three types of variables in java:
1) local variable: inside the body
2) instance variable: inside the class
3) static variable: use static keyword
Rules
for naming a variable
------------------------------------
A
variable name can only have letters (both uppercase (A to Z) and lowercase (a
to z)), digits (0 to 9), $(dollar) and underscore.
Don’t
start with digits but end or mix with digits(numbers)
spaces
or special characters (+, -, *, /, %) are not allowed.
Names
are case sensitive (“name" and “Name"
are different variables)
Reserved
words (like Java keywords, such as int or float) cannot be used
as names
Ex:
Studentname
- valid
Student name
- invalid (space)
Student_name - valid
10student - invalid (start with digits)
Student10
- valid
//Example
program for variable
class
test{
public static void main (String args[]) {
int a=10;
float pi=3.14f;
char ch='a';
String name="Jahab";
System.out.println(a);
System.out.println(pi);
System.out.println(ch);
System.out.println(name);
}
}
---------------------------------------
2.
Keywords:
-------------
- Keywords are the pre-defined reserved
words of java programming.
- Each keyword has a special meaning. It
is always written in lower case.
List of Keywords
------------------------------------------------
3. Java Constant (Literals)
-------------------
- Constant Refer to fixed values. The
Value of Quantity which does not vary or alter throughout the program. These
fixed values are also called Literals
Types
of Literals (Constant)
//Example program for
constant
class
test{
public static void main (String args[]) {
final int a=10;
final float pi=3.14f;
final char ch='a';
final String name="Jahab";
System.out.println(a);
System.out.println(pi);
System.out.println(ch);
System.out.println(name);
} }
-----------------------------------------------
4.
Java Operators
-----------------------
·
Operator in Java is a symbol that is used to perform operations.
For example: +, -, *, / etc.
Types of Operators
----------------------
1. Arithmetic Operator : +, -, *, /, %
2. Relational Operator : >,>=, <, <=, ==, !=
3. Logical operator : &&, ||, !
4. Increment (++) or decrement (--) operator or Unary
Operator (-): ++,--, -
5. Assignment
Operator : =, +=, -=, *=, /=, %=
6. Ternary Operator or conditional operator: ( ?: )
7. Bitwise
Operators : &, |, ^, ~, <<, >>, >>>
--------------------------
//Example
program for Arithmetic operators: +, -, *, /, %
class test{
public static void main (String args[]) {
int a = 10, b = 30;
// Arithmetic operators
System.out.println("a + b = "
+ (a + b));
System.out.println("a - b = "
+ (a - b));
System.out.println("a * b = "
+ (a * b));
System.out.println("a / b = "
+ (a / b));
System.out.println("a % b = "
+ (a % b));
}
}
--------
//Example program for
Relational operators: >,>=, <, <=, ==, !=
class test{
public static void main (String args[]) {
int num1 = 10, num2 = 30;
// Relational operators
System.out.println("num1 > num2 is " + (num1 > num2));
System.out.println("num1
< num2 is " + (num1 < num2));
System.out.println("num1
>= num2 is " + (num1 >= num2));
System.out.println("num1
<= num2 is " + (num1 <= num2));
System.out.println("num1
== num2 is " + (num1 == num2));
System.out.println("num1
!= num2 is " + (num1 != num2));
}
}
---------
//Example program for
Logical operators: &&,
||, !
class test{
public static void main (String args[]) {
int x = 10, y = 30, z = 5;
// Logical operators
System.out.println(x > y && x
> z);
System.out.println(x > y || x >
z);
System.out.println(!(x > y || x >
z));
}
}
----------------
//Example program for
Increment & Decrement (Unary Operator): ++, --, -
class test{
public static void main (String args[]) {
int x = 5;
System.out.println(++x);
System.out.println(x++);
System.out.println(--x);
System.out.println(x--);
System.out.println(-x);
}
}
------------------
//Example program for
Assignment operators: =, +=, -=, *=, /=, %=
class test{
public static void main (String args[]) {
int x = 5;
x += 3;
System.out.println(x);
x -= 3;
System.out.println(x);
x /= 3;
System.out.println(x);
x *= 3;
System.out.println(x);
x %= 3;
System.out.println(x);
int a=b=c=10; //Multiple Assignment
}
}
-----------------
//Example program for
Ternary or Conditional operators: ?:
class test{
public static void main (String args[]) {
int a=5,b=10;
int max = (a>b) ? a : b ;
System.out.println(max);
}
}
----------------
//Example program for Bitwise
operators: &, |,
^, ~, <<, >>, >>>
class test{
public static void main (String args[]) {
int x = 10, y = 8;
System.out.println(x&y);
System.out.println(x|y);
System.out.println(x^y);
System.out.println(~x);
} }
---------------------------------------------------------------------------
5. Java Separators
-----------------------
The
separators in Java is also known as punctuators. Separators help define
the structure of a program.
--------------------------------------------------------------------------------------------
6. Java Comments
------------------------
Java Comments used to specify information about the
program inside our Java code. The Java compiler treats comments as whitespaces.
Types of Java Comments:
1. Single Line comment:
·
It begins with a
pair of forwarding slashes (//).
·
It is used to
refer program title, Author name and single line description
Ex: //Program 1: display your
name
2.
Multi-line Comment (block):
·
It begins with /*
and continues until it founds */.
·
It is used to
program description (paragraph format)
Ex: /*
The code below will print the
words Hello to the screen
The code below will print the
words Hello to the screen
The code below will print the
words Hello to the screen
The code below will print the
words Hello to the screen
*/
----------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12) Java Datatypes
·
Data types specify the different sizes and
values that can be stored in the variable.
There are two types of data
types in Java:
- Primitive data types: The primitive data types
include boolean, char, byte, short,
int, long, float and double.
- Non-primitive data types: The non-primitive data
types include Classes, Interfaces, and Arrays.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13) Java Input and Output Statements
Input
Statement:
Input (getting
data from user through keyboard) is any information that is needed by your
program to complete its execution. (Read input from console)
Scanner
class:
The Scanner class
is used to get user input, and it is found in the java.util package.
Scanner class is a simple
text scanner which can parse primitive types (like int, float, double) and
strings
1)
Import Scanner class:
import java.util.Scanner;
2)
Create a Scanner Object in Java
Scanner obj = new Scanner(System.in);
List of Scanner Class Methods
Method |
Description |
nextBoolean() |
Reads a boolean value from the
user |
nextByte() |
Reads a byte value from the user |
nextDouble() |
Reads a double value from the
user |
nextFloat() |
Reads a float value from the user |
nextInt() |
Reads a int value from the user |
nextLine() |
Reads a String value from the
user |
nextLong() |
Reads a long value from the user |
nextShort() |
Reads a short value from the user |
//Example
program from Scanner class
import
java.util.Scanner;
class
test {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter name: ");
String name = obj.nextLine(); //String input
System.out.print("Enter Age: ");
int age = obj.nextInt(); // Integer
System.out.print("Enter Salary: ");
double salary = obj.nextDouble(); //Floating point
// Output input by user
System.out.println("Name: " +
name);
System.out.println("Age: " +
age);
System.out.println("Salary: " +
salary);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14) Java Statements
·
In Java, a statement is an
executable instruction that tells the compiler what to perform. It forms a
complete command to be executed and can include one or more expressions.
Types of Statements
- Expression Statements
- Declaration Statements
- Control Statements
1.
Expression
Statements
·
Expression is an essential building block of any Java program.
·
In Java, expression is the combination of values,
variables, operators, and method calls.
Ex:
Assign
values
int a
= 10;
float pi=3.14f;
char ch='a';
String name="Jahab";
Expression
c = a + b;
v = 1/3 * 3.14 * r * r * h;
b = (6 + 9);
------------------------------------------------
2.
Declaration
Statements
·
In declaration statements, we declare variables and constants
by specifying their data type and name. A variable holds a value that is going
to use in the Java program.
For example:
int a,b,c;
float pi;
char ch;
String name;
---------------------------------------------------
·
Control statements decide the flow (order or sequence of
execution of statements) of a Java program. In Java, statements are parsed from
top to bottom.
·
Therefore, using the control flow statements can interrupt a
particular section of a program based on a certain condition.
-------------------------------------------
Example
of Statement
//declaration statement
int number;
//expression statement
number = 412;
//control flow statement
if (number > 100) {
System.out.println(number + " is greater than 100");
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15) Java Example Programs
//Program
1: Addition of two numbers(Integer)
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner dis = new Scanner(System.in);
System.out.print("Enter First value :");
int a = dis.nextInt();
System.out.print("Enter Second value :");
int b = dis.nextInt();
int add = a + b;
System.out.println("Addition is :"+add);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Program
2: Addition of two numbers(Float)
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner dis = new Scanner(System.in);
System.out.print("Enter First value :");
float
a = dis.nextFloat();
System.out.print("Enter Second value :");
float
b = dis.nextFloat();
float add = a + b;
System.out.println("Addition is :"+add);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Program
3: Simple Interest calculations
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner dis = new Scanner(System.in);
System.out.print("Enter Principle Amount :");
float p = dis.nextFloat();
System.out.print("Enter No of years :");
float n = dis.nextFloat();
System.out.print("Enter Rate of interest :");
float r = dis.nextFloat();
float si = p * n * r / 100;
System.out.println("Simple Interest is :"+si);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Program
4: Volume of cone
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner dis = new Scanner(System.in);
System.out.print("Enter Radious value :");
float r = dis.nextFloat();
System.out.print("Enter Height value :");
float h = dis.nextFloat();
double v = (1.0/3.0) * Math.PI * Math.pow(r,2) * h;
System.out.println("Volume of Cone is :"+v);
System.out.printf("Volume of Cone is : %.2f",v);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment