SlideShare a Scribd company logo
INTRODUCTION TO JAVA
19IS6IEJVA
INTRODUCTION
• Java is a Computer Programming Language
• Java is related to C++, which is a direct
descendent of ‘C’
• Birth of modern programming language
– Machine Language
– Assembly Level Language
– High-Level Language
• FORTRAN
• BASIC
• COBOL
INTRODUCTION
• ‘C’ Programming language was invented and
implemented by “Dennis Ritchie”
– Structured language
– Programmer is flexible to program related to
systems
– But to manage increasing complexity as the size of
the program reaches a certain threshold, C++ is a
response to that need
INTRODUCTION
• ‘C++’ was invented by “Bjarne Stroustrup”
• It is “Object-Oriented Programming” Language
• Added features
– Enabled certain thresholds to be broken
– Manageable for complex and large programs
• But has certain drawbacks like security and
portability
INTRODUCTION
• The primary motivation was the need for a
platform-independent (that is, architecture-
neutral) language that could be used to create
software to be embedded in various consumer
electronic devices, such as microwave ovens
and remote controls.
• “James Gosling” initiated Java in June 1991 for
use in one of his many set-top box projects
• Initially Java was called as “Oak” language
INTRODUCTION
• CPU as a controller – C/ C++ has been designed for a specific controller
• So, there was a need for 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.
• With the emergence of the World Wide Web, Java was propelled to
the forefront of computer language design, because the Web, too,
demanded portable programs.
• Java was initially designed to solve on a small scale could also be
applied to the Internet on a large scale. This realization caused the
focus of Java to switch from consumer electronics to Internet
programming.
Java’s Magic : The Bytecode
The way other languages work
(C, C++, …)
 Source code is written
 Source code is ported to every different
platform
 Source code is compiled into platform
specific machine code (or binaries)
 Binaries execute on a single platform
Source Code
Linux port Windows port Mac port
Linux
Compile
Windows
Compile
Mac
Compile
Linux binary Win binary Mac binary
Linux
Machine
Windows
Machine Mac Machine
Java: Write once, compile, run
anywhere!
 Java is compiled into machine
independent bytecode class files
 Bytecode is interpreted by the Java Virtual
Machine (JVM)
 JVM executes the step-by-step
instructions given to it from the bytecode
 JVM is specific to each computer
architecture (Linux JVM, Windows JVM,
Mac JVM).
Source Code
Java
Compile
Linux JVM Win JVM Mac JVM
Linux
Machine
Windows
Machine Mac Machine
How Java Does it
Java Buzzwords
12
Java is a language characterized by buzzwords
buzzword: A trendy word or phrase that is used more to impress than
explain
1. Simple
2. Secure
3. Portable
4. Object-Oriented
5. Robust
6. Multithreaded
7. Architecture-Neutral
8. Interpreted
9. High Performance
10. Distributed
11. Dynamic
Simple
• Java is designed to be easy for the
professional programmer to learn and use
• experienced C/ C++ programmer, moving
to Java will require very little effort.
• Also, some of the confusing concepts from
C/ C++ are removed in Java. (like Pointers,
Operator Overloading)
Secure
• Programs are confined to the Java execution environment
and cannot access other parts of the computer.
• So, when using Java compatible web browser, the attack of
viral infection or malicious intent will be minimal
Portable
• Many types of computers and operating systems are in
use throughout the world—and many are connected to
the Internet.
• For programs to be dynamically downloaded to all the
various types of platforms connected to the Internet,
some means of generating portable executable code is
needed. The same mechanism that helps ensure
security also helps create portability.
• Indeed, Java's solution to these two problems is both
elegant and efficient.
Object-Oriented
• A clean, usable, pragmatic approach to objects, not
restricted by the need for compatibility with other
languages.
• The object model in Java is simple and easy to
extend, while primitive types, such as integers, are
kept as high-performance nonobjects.
Robust
• Restricts the programmer to find the mistakes early,
performs compile-time (strong typing) and run-time
(exception-handling) checks, manages memory
automatically.
Multithreaded
• supports multi-threaded programming for writing
program that perform concurrent computations
Architecture-neutral
• Java Virtual Machine provides a platform independent
environment for the execution of Java byte code
• One of the main problems facing programmers is that no
guarantee exists that if you write a program today, it will
run tomorrow—even on the same machine. Operating
system upgrades, processor upgrades, and changes in
core system resources can all combine to make a program
malfunction. The Java designers made several hard
decisions in the Java language and the Java Virtual
Machine in an attempt to alter this situation. Their goal
was “write once; run anywhere, any time, forever.”
Interpreted and high-performance
• Java programs are compiled into an intermediate
representation – byte code:
a) can be later interpreted by any JVM
b) can be also translated into the native machine code for
efficiency
Distributed System
• A distributed system is one where multiple separate
computer systems are involved
– the Internet is a very large distributed system
• interconnected collection of computer networks
• heterogeneous components
• large number of services: WWW, file services,
audio/video channels
• Java was designed for the web as it handles TCP/IP Protocol
Dynamic
• Java is said to be dynamic because the java byte code may be
dynamically updated on a running system and it has a
dynamic memory allocation and deallocation (objects and
garbage collector).
The first sample program
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
Save the filename with classname. Here it is Example.java
CONT….
• Compiling: To compile the Example program, execute the
compiler, javac, specifying the name of the source file on the
command line, as shown here:
• C:> javac Example.java
• The javac compiler creates a file called Example.class that
contains the bytecode version of the program.
• The output of javac is not code that can be directly executed.
To actually run the program, you must use the Java application
launcher, called java. To do so, pass the class name Example
as a command-line argument, as shown here:
• C:>java Example
Example 2:
/* Here is another short example. Call this file "Example2.java". */
class Example2
{
public static void main(String args[])
{
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
When you run this program, you will see the following output:
This is num: 100
The value of num * 2 is 200
• It is important to state at the outset that Java
is a strongly typed language.
• Data types: java defines 8 primitive types of
data:
• Integers - byte , short, int, long
• Floating-Point Types - float, double
• char
• boolean
Data Types
Integers
• Java defines four integer types -
byte, short, int, and long.
• The width and ranges of these integer types
vary widely, as shown in this table:
byte :
• The smallest integer type is byte.
• This is a signed 8-bit type that has a range from –128 to 127.
• Variables of type byte are especially useful when you’re working with
a stream of data from a network or file.
• Byte variables are declared by use of the byte keyword.
• For example, the following declares two byte variables called b and c:
– byte b, c;
short
• short is a signed 16-bit type.
• It has a range from –32,768 to 32,767.
• It is probably the least-used Java type. Here are some examples
of short variable declarations:
– short s;
int
• The most commonly used integer type is int.
• It is a signed 32-bit type that has a range from
–2,147,483,648 to 2,147,483,647.
– Int a,b,c;
long
long is a signed 64-bit type and is useful for
those occasions where an int type is not
large enough to hold the desired value
• Floating-point numbers, also known as real numbers, are used
when evaluating expressions that require fractional precision.
• For example, calculations such as square root, or
transcendental such as sine and cosine, result in a value
whose precision requires a floating-point type.
• here are two kinds of floating-point types, float and double,
which represent single and double-precision numbers,
respectively. Their width and ranges are shown here:
Floating-Point Types
float
• float specifies a single-precision value that uses 32 bits of storage.
• Variables of type float are useful when you need a fractional
component, but don’t require a large degree of precision. For
example, float can be useful when representing dollars and cents.
• Here are some example float variable declarations:
– float hightemp, lowtemp;
double
• uses 64 bits to store a value.
• transcendental math functions, such as sin( ), cos( ), and sqrt( ),
return double values. When you need to maintain accuracy over
many iterative calculations, or are manipulating large-valued
numbers, double is the best choice.
Characters
• Java uses Unicode to represent characters.
• Thus, in Java char is a 16-bit type. The range of
a char is 0 to 65,536.
Booleans
• boolean, is for logical values. It can have only
one of two possible values, true or false. This
is the type returned by all relational operators,
as in the case of a < b.
Variables
• variable is the basic unit of storage in a Java
program.
• Declaring a Variable
examples of variable declarations of various types.
– int a, b, c; // declares three ints, a, b, and c.
– int d = 3, e, f = 5; // initializing d and f.
– byte z = 22; // initializes z.
– double pi = 3.14159; // declares an approximation of pi.
– char x = 'x'; // the variable x has the value 'x'.
The if Statement
Syntax:
if(condition)
statement;
Here, condition is a Boolean expression.
Here is an example:
if(num< 100)
System.out.println("num is less than 100");
/* Demonstrate the if. Call this file "IfSample.java". */
classIfSample
{
public static void main(String args[])
{
int x, y;
x = 10;
y = 20;
if(x < y)
System.out.println("x is less than y");
x = x * 2;
if(x == y)
System.out.println("x now equal to y");
x = x * 2;
if(x > y)
System.out.println("x now greater than y");
if(x == y)
System.out.println("you won't see this");
}
}
The output generated by this program is shown here:
x is less than y
x now equal to y
x now greater than y
The for Loop
• for (initialization; condition; iteration)
statement;
• The initialization portion of the loop sets a
loop control variable to an initial value. The
condition is a Boolean expression that tests the
loop control variable.
• If the outcome of that test is true, the for loop
continues to iterate. If it is false, the loop
terminates. The iteration expression determines
how the loop control variable is changed each
time the loop iterates.
4.Program that illustrates the for loop:
/* Demonstrate the for loop.
Call this file "ForTest.java". */
class ForTest
{
public static void main(String args[ ])
{
int x;
for(x = 0; x<5; x = x+1)
System.out.println("This is x: " + x);
}
}
/*This program generates the following output:
This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4*/
Lexical Issues
• Java programs are a collection of
• whitespace,
• identifiers,
• literals,
• comments,
• operators,
• separators, and
• keywords.
it is time to more formally describe the atomic elements of
Java.
• Whitespace
– In Java, whitespace is a space, tab, or newline.
– do not need to follow any special indentation rules.
• Identifiers
– Identifiers are used to name things, such as classes,
variables, and methods.
– An identifier may be any descriptive sequence of
uppercase and lowercase letters, numbers, or the
underscore and dollar-sign characters.
– They must not begin with a number
Valid identifiers are
AvgTemp count a4 $test this_is_ok
Invalid identifier
2count high-temp Not/ok
• Literals
– A constant value in Java is created by using a literal representation of it.
– For example, here are some literals:
• 100 –Integer Constant
• 98.6 –Float Constant
• 'X' - Character Constant
• "This is a test“ – String Constant
• Comments
There are three types of comments defined by Java
– Single line (represented as //)
– Multiline (Starts and ends with /* */)
– Documentation: This type of comment is used to produce an HTMLfile that
documents your program. The documentation comment begins with a /** and
ends with a */.
Enter a number:4 4 is even number enter a number:5
• Separators
– In Java, there are a few characters that are used as separators.
– The most commonly used separator in Java is the semicolon.
• The Java Keywords
– There are 50 keywords currently defined in
the Java language(shown in below table). These
keywords, combined with the syntax of the
operators and separators, form the foundation
The simple program of c language for loop where we are
printing the from 1 to 10
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d n",i);
}
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
C Functions
• In c, we can divide a large program into the
basic building blocks known as function.
• The function contains the set of programming
statements enclosed by {}.
• The function is also known as procedure
or subroutinein other programming languages.
Types of Functions
There are two types of functions in C programming:
Library Functions: are the functions which are
declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
User-defined functions: are the functions which
are created by the C programmer, so that he/she
can use it many times. It reduces the complexity
of a big program and optimizes the code.
Example for Function to add two numbers
#include<stdio.h>
int sum();
void main()
{
int result;
printf("nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Output
Going to calculate the sum of two numbers:
Enter two numbers 10
24
The sum is 34
Function Aspects
There are three aspects of a C function.
• Function declaration A function must be declared globally in a c
program to tell the compiler about the function name, function
parameters, and return type.
– return_type function_name (argument list);
•
Function call Function can be called from anywhere in the program.
The parameter list must not differ in function calling and function
declaration. We must pass the same number of functions as it is
declared in the function declaration.
– function_name (argument_list)
•
Function definition It contains the actual statements which are to be
executed. It is the most important aspect to which the control comes
when the function is called. Here, we must notice that only one
value can be returned from the function.
– return_type function_name (argument list) {function body;}
JAVA Module 1______________________.pptx

More Related Content

PPTX
Introduction to Java Basics Programming Java Basics-I.pptx
PPTX
Unit-1_GHD.pptxguguigihihihihihihoihihhi
PPT
javaeanjjisjejrehurfhjhjfeauojksfjdi.ppt
PPT
Comp102 lec 3
PPT
Java SpringMVC SpringBOOT (Divergent).ppt
PPTX
Java Basics.pptx from nit patna ece department
PPTX
Java OOP Concepts 1st Slide
DOCX
OOP-Chap2.docx
Introduction to Java Basics Programming Java Basics-I.pptx
Unit-1_GHD.pptxguguigihihihihihihoihihhi
javaeanjjisjejrehurfhjhjfeauojksfjdi.ppt
Comp102 lec 3
Java SpringMVC SpringBOOT (Divergent).ppt
Java Basics.pptx from nit patna ece department
Java OOP Concepts 1st Slide
OOP-Chap2.docx

Similar to JAVA Module 1______________________.pptx (20)

PPTX
Java platform
PPT
Introduction to Java Programming, Basic Structure, variables Data type, input...
DOCX
Srgoc java
PPTX
Modern_2.pptx for java
ODP
Introduction To Java.
PPT
Unit 2 Java
PPT
Java-Unit-I.ppt
PDF
Core java part1
PDF
OOPS JAVA.pdf
PDF
Java Programming
PPT
Introduction to java programming part 1
PPTX
UNIT 1.pptx
PPTX
Programming in java ppt
PPTX
Programming in java ppt
PPT
Chapter 1 java
PDF
Learning Java Beginning programming with java for dummies First Edition John ...
PDF
java notes.pdf
PPTX
JAVA introduction and basic understanding.pptx
PPTX
object oriented programming unit one ppt
PPTX
What is Java, JDK, JVM, Introduction to Java.pptx
Java platform
Introduction to Java Programming, Basic Structure, variables Data type, input...
Srgoc java
Modern_2.pptx for java
Introduction To Java.
Unit 2 Java
Java-Unit-I.ppt
Core java part1
OOPS JAVA.pdf
Java Programming
Introduction to java programming part 1
UNIT 1.pptx
Programming in java ppt
Programming in java ppt
Chapter 1 java
Learning Java Beginning programming with java for dummies First Edition John ...
java notes.pdf
JAVA introduction and basic understanding.pptx
object oriented programming unit one ppt
What is Java, JDK, JVM, Introduction to Java.pptx
Ad

More from Radhika Venkatesh (10)

PPT
exception handling in java-123456789.ppt
PPTX
Inheritance in java-1234356789101112.pptx
PPTX
Module 3 and 4-javahjhhdhdhddhhdhdhdhd.pptx
PPTX
Module 4-Multithreading-1bzBXzxbbxxb.pptx
PPTX
Module-5-hjhjhjkhdjkhjhjhhhjhjhhhhj.pptx
PPTX
JAVA Module 2jdhgsjdsjdjsddsjjssdjdsjhsdjh.pptx
PPTX
Module--fundamentals and operators in java1.pptx
PPTX
Module 4-packages and exceptions in java.pptx
PPTX
JAVA Module 2____________________--.pptx
PPT
memory systems-module 3 presentation ppt
exception handling in java-123456789.ppt
Inheritance in java-1234356789101112.pptx
Module 3 and 4-javahjhhdhdhddhhdhdhdhd.pptx
Module 4-Multithreading-1bzBXzxbbxxb.pptx
Module-5-hjhjhjkhdjkhjhjhhhjhjhhhhj.pptx
JAVA Module 2jdhgsjdsjdjsddsjjssdjdsjhsdjh.pptx
Module--fundamentals and operators in java1.pptx
Module 4-packages and exceptions in java.pptx
JAVA Module 2____________________--.pptx
memory systems-module 3 presentation ppt
Ad

Recently uploaded (20)

PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Software Engineering and software moduleing
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Soil Improvement Techniques Note - Rabbi
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
Design Guidelines and solutions for Plastics parts
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
introduction to high performance computing
PDF
Visual Aids for Exploratory Data Analysis.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Exploratory_Data_Analysis_Fundamentals.pdf
Safety Seminar civil to be ensured for safe working.
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Fundamentals of Mechanical Engineering.pptx
Software Engineering and software moduleing
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Soil Improvement Techniques Note - Rabbi
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
Design Guidelines and solutions for Plastics parts
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
distributed database system" (DDBS) is often used to refer to both the distri...
introduction to high performance computing
Visual Aids for Exploratory Data Analysis.pdf

JAVA Module 1______________________.pptx

  • 2. INTRODUCTION • Java is a Computer Programming Language • Java is related to C++, which is a direct descendent of ‘C’ • Birth of modern programming language – Machine Language – Assembly Level Language – High-Level Language • FORTRAN • BASIC • COBOL
  • 3. INTRODUCTION • ‘C’ Programming language was invented and implemented by “Dennis Ritchie” – Structured language – Programmer is flexible to program related to systems – But to manage increasing complexity as the size of the program reaches a certain threshold, C++ is a response to that need
  • 4. INTRODUCTION • ‘C++’ was invented by “Bjarne Stroustrup” • It is “Object-Oriented Programming” Language • Added features – Enabled certain thresholds to be broken – Manageable for complex and large programs • But has certain drawbacks like security and portability
  • 5. INTRODUCTION • The primary motivation was the need for a platform-independent (that is, architecture- neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. • “James Gosling” initiated Java in June 1991 for use in one of his many set-top box projects • Initially Java was called as “Oak” language
  • 6. INTRODUCTION • CPU as a controller – C/ C++ has been designed for a specific controller • So, there was a need for 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. • With the emergence of the World Wide Web, Java was propelled to the forefront of computer language design, because the Web, too, demanded portable programs. • Java was initially designed to solve on a small scale could also be applied to the Internet on a large scale. This realization caused the focus of Java to switch from consumer electronics to Internet programming.
  • 7. Java’s Magic : The Bytecode
  • 8. The way other languages work (C, C++, …)  Source code is written  Source code is ported to every different platform  Source code is compiled into platform specific machine code (or binaries)  Binaries execute on a single platform
  • 9. Source Code Linux port Windows port Mac port Linux Compile Windows Compile Mac Compile Linux binary Win binary Mac binary Linux Machine Windows Machine Mac Machine
  • 10. Java: Write once, compile, run anywhere!  Java is compiled into machine independent bytecode class files  Bytecode is interpreted by the Java Virtual Machine (JVM)  JVM executes the step-by-step instructions given to it from the bytecode  JVM is specific to each computer architecture (Linux JVM, Windows JVM, Mac JVM).
  • 11. Source Code Java Compile Linux JVM Win JVM Mac JVM Linux Machine Windows Machine Mac Machine How Java Does it
  • 12. Java Buzzwords 12 Java is a language characterized by buzzwords buzzword: A trendy word or phrase that is used more to impress than explain 1. Simple 2. Secure 3. Portable 4. Object-Oriented 5. Robust 6. Multithreaded 7. Architecture-Neutral 8. Interpreted 9. High Performance 10. Distributed 11. Dynamic
  • 13. Simple • Java is designed to be easy for the professional programmer to learn and use • experienced C/ C++ programmer, moving to Java will require very little effort. • Also, some of the confusing concepts from C/ C++ are removed in Java. (like Pointers, Operator Overloading)
  • 14. Secure • Programs are confined to the Java execution environment and cannot access other parts of the computer. • So, when using Java compatible web browser, the attack of viral infection or malicious intent will be minimal Portable • Many types of computers and operating systems are in use throughout the world—and many are connected to the Internet. • For programs to be dynamically downloaded to all the various types of platforms connected to the Internet, some means of generating portable executable code is needed. The same mechanism that helps ensure security also helps create portability. • Indeed, Java's solution to these two problems is both elegant and efficient.
  • 15. Object-Oriented • A clean, usable, pragmatic approach to objects, not restricted by the need for compatibility with other languages. • The object model in Java is simple and easy to extend, while primitive types, such as integers, are kept as high-performance nonobjects. Robust • Restricts the programmer to find the mistakes early, performs compile-time (strong typing) and run-time (exception-handling) checks, manages memory automatically.
  • 16. Multithreaded • supports multi-threaded programming for writing program that perform concurrent computations Architecture-neutral • Java Virtual Machine provides a platform independent environment for the execution of Java byte code • One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow—even on the same machine. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.”
  • 17. Interpreted and high-performance • Java programs are compiled into an intermediate representation – byte code: a) can be later interpreted by any JVM b) can be also translated into the native machine code for efficiency Distributed System • A distributed system is one where multiple separate computer systems are involved – the Internet is a very large distributed system • interconnected collection of computer networks • heterogeneous components • large number of services: WWW, file services, audio/video channels • Java was designed for the web as it handles TCP/IP Protocol
  • 18. Dynamic • Java is said to be dynamic because the java byte code may be dynamically updated on a running system and it has a dynamic memory allocation and deallocation (objects and garbage collector).
  • 19. The first sample program /* This is a simple Java program. Call this file "Example.java". */ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } } Save the filename with classname. Here it is Example.java
  • 20. CONT…. • Compiling: To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: • C:> javac Example.java • The javac compiler creates a file called Example.class that contains the bytecode version of the program. • The output of javac is not code that can be directly executed. To actually run the program, you must use the Java application launcher, called java. To do so, pass the class name Example as a command-line argument, as shown here: • C:>java Example
  • 21. Example 2: /* Here is another short example. Call this file "Example2.java". */ class Example2 { public static void main(String args[]) { int num; // this declares a variable called num num = 100; // this assigns num the value 100 System.out.println("This is num: " + num); num = num * 2; System.out.print("The value of num * 2 is "); System.out.println(num); } } When you run this program, you will see the following output: This is num: 100 The value of num * 2 is 200
  • 22. • It is important to state at the outset that Java is a strongly typed language. • Data types: java defines 8 primitive types of data: • Integers - byte , short, int, long • Floating-Point Types - float, double • char • boolean Data Types
  • 23. Integers • Java defines four integer types - byte, short, int, and long. • The width and ranges of these integer types vary widely, as shown in this table:
  • 24. byte : • The smallest integer type is byte. • This is a signed 8-bit type that has a range from –128 to 127. • Variables of type byte are especially useful when you’re working with a stream of data from a network or file. • Byte variables are declared by use of the byte keyword. • For example, the following declares two byte variables called b and c: – byte b, c; short • short is a signed 16-bit type. • It has a range from –32,768 to 32,767. • It is probably the least-used Java type. Here are some examples of short variable declarations: – short s;
  • 25. int • The most commonly used integer type is int. • It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. – Int a,b,c; long long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value
  • 26. • Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. • For example, calculations such as square root, or transcendental such as sine and cosine, result in a value whose precision requires a floating-point type. • here are two kinds of floating-point types, float and double, which represent single and double-precision numbers, respectively. Their width and ranges are shown here: Floating-Point Types
  • 27. float • float specifies a single-precision value that uses 32 bits of storage. • Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. For example, float can be useful when representing dollars and cents. • Here are some example float variable declarations: – float hightemp, lowtemp; double • uses 64 bits to store a value. • transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.
  • 28. Characters • Java uses Unicode to represent characters. • Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. Booleans • boolean, is for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, as in the case of a < b.
  • 29. Variables • variable is the basic unit of storage in a Java program. • Declaring a Variable examples of variable declarations of various types. – int a, b, c; // declares three ints, a, b, and c. – int d = 3, e, f = 5; // initializing d and f. – byte z = 22; // initializes z. – double pi = 3.14159; // declares an approximation of pi. – char x = 'x'; // the variable x has the value 'x'.
  • 30. The if Statement Syntax: if(condition) statement; Here, condition is a Boolean expression. Here is an example: if(num< 100) System.out.println("num is less than 100");
  • 31. /* Demonstrate the if. Call this file "IfSample.java". */ classIfSample { public static void main(String args[]) { int x, y; x = 10; y = 20; if(x < y) System.out.println("x is less than y"); x = x * 2; if(x == y) System.out.println("x now equal to y"); x = x * 2; if(x > y) System.out.println("x now greater than y"); if(x == y) System.out.println("you won't see this"); } } The output generated by this program is shown here: x is less than y x now equal to y x now greater than y
  • 32. The for Loop • for (initialization; condition; iteration) statement; • The initialization portion of the loop sets a loop control variable to an initial value. The condition is a Boolean expression that tests the loop control variable. • If the outcome of that test is true, the for loop continues to iterate. If it is false, the loop terminates. The iteration expression determines how the loop control variable is changed each time the loop iterates.
  • 33. 4.Program that illustrates the for loop: /* Demonstrate the for loop. Call this file "ForTest.java". */ class ForTest { public static void main(String args[ ]) { int x; for(x = 0; x<5; x = x+1) System.out.println("This is x: " + x); } } /*This program generates the following output: This is x: 0 This is x: 1 This is x: 2 This is x: 3 This is x: 4*/
  • 34. Lexical Issues • Java programs are a collection of • whitespace, • identifiers, • literals, • comments, • operators, • separators, and • keywords. it is time to more formally describe the atomic elements of Java.
  • 35. • Whitespace – In Java, whitespace is a space, tab, or newline. – do not need to follow any special indentation rules. • Identifiers – Identifiers are used to name things, such as classes, variables, and methods. – An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. – They must not begin with a number Valid identifiers are AvgTemp count a4 $test this_is_ok Invalid identifier 2count high-temp Not/ok
  • 36. • Literals – A constant value in Java is created by using a literal representation of it. – For example, here are some literals: • 100 –Integer Constant • 98.6 –Float Constant • 'X' - Character Constant • "This is a test“ – String Constant • Comments There are three types of comments defined by Java – Single line (represented as //) – Multiline (Starts and ends with /* */) – Documentation: This type of comment is used to produce an HTMLfile that documents your program. The documentation comment begins with a /** and ends with a */. Enter a number:4 4 is even number enter a number:5
  • 37. • Separators – In Java, there are a few characters that are used as separators. – The most commonly used separator in Java is the semicolon.
  • 38. • The Java Keywords – There are 50 keywords currently defined in the Java language(shown in below table). These keywords, combined with the syntax of the operators and separators, form the foundation
  • 39. The simple program of c language for loop where we are printing the from 1 to 10 #include<stdio.h> int main(){ int i=0; for(i=1;i<=10;i++){ printf("%d n",i); } return 0; } Output 1 2 3 4 5 6 7 8 9 10
  • 40. C Functions • In c, we can divide a large program into the basic building blocks known as function. • The function contains the set of programming statements enclosed by {}. • The function is also known as procedure or subroutinein other programming languages.
  • 41. Types of Functions There are two types of functions in C programming: Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
  • 42. Example for Function to add two numbers #include<stdio.h> int sum(); void main() { int result; printf("nGoing to calculate the sum of two numbers:"); result = sum(); printf("%d",result); } int sum() { int a,b; printf("nEnter two numbers"); scanf("%d %d",&a,&b); return a+b; } Output Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34
  • 43. Function Aspects There are three aspects of a C function. • Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type. – return_type function_name (argument list); • Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration. – function_name (argument_list) • Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function. – return_type function_name (argument list) {function body;}