SlideShare a Scribd company logo
Java Introduction
Introduction
• Computer language innovation and
development occurs for two fundamental
reasons:
– To adapt to changing environments and uses
– To implement refinements and improvements in
the art of programming
• From C, Java derives its syntax.
• object-oriented features were influenced by
C++.
• The Birth of Modern Programming: C
– The creation of C was a direct result of the need
for a structured, efficient, high level language that
could replace assembly code when creating
systems programs.
– FORTRAN could be used to write fairly efficient
programs for scientific applications, it was not
very good for systems code
• Assembly language can be used to produce highly
efficient programs, but it is not easy to learn or
use effectively.
• Further, debugging assembly code can be quite
difficult.
• Invented and first implemented by Dennis Ritchie
on a DEC PDP-11 running the UNIX operating
system, C was the result of a development
process that started with an older language called
BCPL, developed by Martin Richards
• BCPL influenced a language called B, invented
by Ken Thompson, which led to the
development of C in the 1970s.
• C was formally standardized in December
1989, when the American National Standards
Institute (ANSI) standard for C was adopted.
• C is a successful and useful language, you
might ask why a need for something else
existed. The answer is complexity.
• The 1960s gave birth to structured programming
• even with structured programming methods,
once a project reaches a certain size, its
complexity exceeds what a programmer can
manage.
• a new way to program was invented, called
object-oriented programming (OOP)
• OOP is a programming methodology that helps
• organize complex programs through the use of
inheritance, encapsulation, and Polymorphism.
• C++ was invented by Bjarne Stroustrup in
1979, while he was working at Bell
• Laboratories in Murray Hill, New Jersey.
Stroustrup initially called the new language
“C with Classes.”
• in 1983, the name was changed to C++.
• C++ extends C by adding object-oriented
features.
• Java was conceived by James Gosling, Patrick
Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991
• This language was initially called “Oak” but
was renamed “Java” in 1995.
The Creation of Java
• An application is a program that runs on your
computer, under the operating system of that
computer.
• An applet is an application designed to be
transmitted over the Internet and executed by
a Java-compatible Web browser.
• 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
Java Applets and Applications
• every time that you download a “normal”
program, you are risking a viral infection.
• In addition to viruses, another type of
malicious program exists that must be
guarded against.
• Java answers both of these concerns by
providing a “firewall” between a networked
application and your computer.
• When you use a Java-compatible Web
browser, you can safely download
Security
• Many types of computers and operating
systems are in use throughout the world
• Many are connected to the Internet.
Portability
• Bytecode is a highly optimized set of
instructions designed to be executed by the
Java run-time system, which is called the Java
Virtual Machine (JVM).
• That is, in its standard form, the JVM is an
interpreter for bytecode.
• Translating a Java program into bytecode
helps makes it much easier to run a
• program in a wide variety of environments.
The Bytecode
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
The Java Buzzwords
•Architecture-neutral
•Interpreted
• High performance
• Distributed
• Dynamic
• Integers This group includes byte, short, int, and
long, which are for whole valued signed numbers.
• Floating-point numbers This group includes float and
double, which represent numbers with fractional
precision.
• Characters This group includes char, which
represents symbols in a character set, like letters
and numbers.
• Boolean This group includes boolean, which is a
special type for representing true/false values.
Data type
• Java does not support unsigned, positive-only
integers
Integers
• Floating-point numbers, also known as real
numbers, are used when evaluating
expressions that require fractional precision.
• All transcendental math functions, such as
sin(), cos( ), and sqrt( ), return double values.
Floating-Point Types
• Java uses Unicode to represent characters.
Unicode defines a fully international character
set that can represent all of the characters
found in all human languages.
• Thus, in Java char is a 16-bit type. The range
of a char is 0 to 65,536.
• There are no negative chars.
Characters
• Java has a simple type, called Boolean, for
logical values
• It can have only one of two possible values,
true or false.
• This is the type returned by all relational
operators
Booleans
• Java Is a Strongly Typed Language
• Part of Java’s safety and robustness every variable has a
type, every expression has a type, and every type is strictly
defined.
• Second, all assignments, whether explicit or via parameter
passing in method calls, are checked for type compatibility.
• There are no automatic coercions or conversions of
conflicting types as in some languages.
• The Java compiler checks all expressions and parameters to
ensure that the types are compatible.
• Any type mismatches are errors that must be corrected
before the compiler will finish compiling the class.
Java Is a Strongly Typed Language
• A variable is defined by the combination of an
identifier, a type, and an optional initializer.
• Declaring a Variable
– type identifier [ = value][, identifier [= value] ...] ;
• In Java, all variables must be declared before
they can be used.
Variables
• Java allows variables to be declared within any
block.
• A block defines a scope. Thus, each time you
start a new block, you are creating a new
scope.
• A scope determines what objects are visible to
other parts of your program.
• It also determines the lifetime of those
objects.
The Scope and Lifetime of
Variables
• When you declare a variable within a scope,
you are localizing that variable and protecting
it from unauthorized access
• Objects declared in the outer scope will be
visible to code within the inner scope.
• However, the reverse is not true. Objects
declared within the inner scope will not be
visible outside it. (SAMPLE WITH SWAP PROGRAM)
• If the two types are compatible, then Java will
perform the conversion automatically.
• it is still possible to obtain a conversion
between incompatible types.
• an automatic type conversion will take place if
the following two conditions are met:
– The two types are compatible.
– The destination type is larger than the source
type.
Type Conversion and Casting
• It has this general form:
– (target-type) value
• if the size of the whole number
• component is too large to fit into the target
integer type, then that value will be reduced
modulo the target type’s range.
– Conversion of int to byte.
– i and b 257 1
• First, all byte and short values are promoted
to int, as just described.
• Then, if one operand is a long, the whole
expression is promoted to long.
• If one operand is a float, the entire expression
is promoted to float.
• If any of the operands is double, the result is
double.
type promotion rules
• An array is a group of like-typed variables that
are referred to by a common name.
• Is a homogeneous data type
Arrays
• The general form of a one dimensional array
declaration is
– type array-var[ ];
• Here, type declares the base type of the array.
• the value of array is set to null
• new is a special operator that allocates
memory.
– array-var = new type[size];
One-Dimensional Arrays
• First, you must declare a variable of the
desired array type.
• Second, you must allocate the memory that
will hold the array, using new, and assign it to
the array variable.
• Thus, in Java all arrays are dynamically
allocated.
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
• In Java, multidimensional arrays are actually
arrays of arrays.
• a two-dimensional array variable called twoD.
– int twoD[][] = new int[4][5];
Multidimensional Arrays
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
• If you need only specify the memory for the
first (leftmost) dimension.
• You can allocate the remaining dimensions
separately.
– int twoD[][] = new int[4][];
– twoD[0] = new int[5];
– twoD[1] = new int[5];
– twoD[2] = new int[5];
– twoD[3] = new int[5];
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
0
1 2
3 4 5
6 7 8 9
– type[ ] var-name;
• Eg:
– int al[] = new int[3];
– int[] a2 = new int[3];
• The following declarations are also equivalent:
– char twod1[][] = new char[3][4];
– char[][] twod2 = new char[3][4];
Alternative Array Declaration
Syntax
• Java does not support or allow pointers.
• Java does not support pointers that can be
accessed and/or modified by the programmer.
• Java cannot allow pointers, because doing so
would allow Java applets to breach the
firewall between the Java execution
environment and the host computer.
Pointers in Java

More Related Content

What's hot (9)

Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variables
 
Maths&programming forartists wip
Maths&programming forartists wipMaths&programming forartists wip
Maths&programming forartists wip
 
Python for katana
Python for katanaPython for katana
Python for katana
 
Mel for beginners
Mel for beginnersMel for beginners
Mel for beginners
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 

Similar to Java introduction

Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
 
java tutorial for beginners learning.ppt
java tutorial for beginners learning.pptjava tutorial for beginners learning.ppt
java tutorial for beginners learning.ppt
usha852
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
Chapter-introduction about java programming
Chapter-introduction about java programmingChapter-introduction about java programming
Chapter-introduction about java programming
DrRajeshkumarPPatel
 

Similar to Java introduction (20)

intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptx
 
Unit 1
Unit 1Unit 1
Unit 1
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Csharp
CsharpCsharp
Csharp
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
java tutorial for beginners learning.ppt
java tutorial for beginners learning.pptjava tutorial for beginners learning.ppt
java tutorial for beginners learning.ppt
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
 
Chapter-introduction about java programming
Chapter-introduction about java programmingChapter-introduction about java programming
Chapter-introduction about java programming
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
 
Java Basics.ppt
Java Basics.pptJava Basics.ppt
Java Basics.ppt
 
C programming
C programmingC programming
C programming
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
C++ programming Assignment Help
C++ programming Assignment HelpC++ programming Assignment Help
C++ programming Assignment Help
 
Oop(object oriented programming)
Oop(object oriented programming)Oop(object oriented programming)
Oop(object oriented programming)
 
COMPUTER PROGRAMMING LANGUAGE.pptx
COMPUTER PROGRAMMING LANGUAGE.pptxCOMPUTER PROGRAMMING LANGUAGE.pptx
COMPUTER PROGRAMMING LANGUAGE.pptx
 

More from GaneshKumarKanthiah (11)

Software testing regression testing
Software testing  regression testingSoftware testing  regression testing
Software testing regression testing
 
Software testing acceptance testing
Software testing  acceptance testingSoftware testing  acceptance testing
Software testing acceptance testing
 
Software testing performance testing
Software testing  performance testingSoftware testing  performance testing
Software testing performance testing
 
Software testing introduction
Software testing  introductionSoftware testing  introduction
Software testing introduction
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java packages
Java packagesJava packages
Java packages
 
Java interface
Java interfaceJava interface
Java interface
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java awt
Java awtJava awt
Java awt
 
Java applet
Java appletJava applet
Java applet
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

Java introduction

  • 2. Introduction • Computer language innovation and development occurs for two fundamental reasons: – To adapt to changing environments and uses – To implement refinements and improvements in the art of programming • From C, Java derives its syntax. • object-oriented features were influenced by C++.
  • 3. • The Birth of Modern Programming: C – The creation of C was a direct result of the need for a structured, efficient, high level language that could replace assembly code when creating systems programs. – FORTRAN could be used to write fairly efficient programs for scientific applications, it was not very good for systems code
  • 4. • Assembly language can be used to produce highly efficient programs, but it is not easy to learn or use effectively. • Further, debugging assembly code can be quite difficult. • Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX operating system, C was the result of a development process that started with an older language called BCPL, developed by Martin Richards
  • 5. • BCPL influenced a language called B, invented by Ken Thompson, which led to the development of C in the 1970s. • C was formally standardized in December 1989, when the American National Standards Institute (ANSI) standard for C was adopted. • C is a successful and useful language, you might ask why a need for something else existed. The answer is complexity.
  • 6. • The 1960s gave birth to structured programming • even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage. • a new way to program was invented, called object-oriented programming (OOP) • OOP is a programming methodology that helps • organize complex programs through the use of inheritance, encapsulation, and Polymorphism.
  • 7. • C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell • Laboratories in Murray Hill, New Jersey. Stroustrup initially called the new language “C with Classes.” • in 1983, the name was changed to C++. • C++ extends C by adding object-oriented features.
  • 8. • Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991 • This language was initially called “Oak” but was renamed “Java” in 1995. The Creation of Java
  • 9. • An application is a program that runs on your computer, under the operating system of that computer. • An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser. • 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 Java Applets and Applications
  • 10. • every time that you download a “normal” program, you are risking a viral infection. • In addition to viruses, another type of malicious program exists that must be guarded against. • Java answers both of these concerns by providing a “firewall” between a networked application and your computer. • When you use a Java-compatible Web browser, you can safely download Security
  • 11. • Many types of computers and operating systems are in use throughout the world • Many are connected to the Internet. Portability
  • 12. • Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). • That is, in its standard form, the JVM is an interpreter for bytecode. • Translating a Java program into bytecode helps makes it much easier to run a • program in a wide variety of environments. The Bytecode
  • 13. • Simple • Secure • Portable • Object-oriented • Robust • Multithreaded The Java Buzzwords •Architecture-neutral •Interpreted • High performance • Distributed • Dynamic
  • 14. • Integers This group includes byte, short, int, and long, which are for whole valued signed numbers. • Floating-point numbers This group includes float and double, which represent numbers with fractional precision. • Characters This group includes char, which represents symbols in a character set, like letters and numbers. • Boolean This group includes boolean, which is a special type for representing true/false values. Data type
  • 15. • Java does not support unsigned, positive-only integers Integers
  • 16. • Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. • All transcendental math functions, such as sin(), cos( ), and sqrt( ), return double values. Floating-Point Types
  • 17. • Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. • Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. • There are no negative chars. Characters
  • 18. • Java has a simple type, called Boolean, for logical values • It can have only one of two possible values, true or false. • This is the type returned by all relational operators Booleans
  • 19. • Java Is a Strongly Typed Language • Part of Java’s safety and robustness every variable has a type, every expression has a type, and every type is strictly defined. • Second, all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. • There are no automatic coercions or conversions of conflicting types as in some languages. • The Java compiler checks all expressions and parameters to ensure that the types are compatible. • Any type mismatches are errors that must be corrected before the compiler will finish compiling the class. Java Is a Strongly Typed Language
  • 20. • A variable is defined by the combination of an identifier, a type, and an optional initializer. • Declaring a Variable – type identifier [ = value][, identifier [= value] ...] ; • In Java, all variables must be declared before they can be used. Variables
  • 21. • Java allows variables to be declared within any block. • A block defines a scope. Thus, each time you start a new block, you are creating a new scope. • A scope determines what objects are visible to other parts of your program. • It also determines the lifetime of those objects. The Scope and Lifetime of Variables
  • 22. • When you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access • Objects declared in the outer scope will be visible to code within the inner scope. • However, the reverse is not true. Objects declared within the inner scope will not be visible outside it. (SAMPLE WITH SWAP PROGRAM)
  • 23. • If the two types are compatible, then Java will perform the conversion automatically. • it is still possible to obtain a conversion between incompatible types. • an automatic type conversion will take place if the following two conditions are met: – The two types are compatible. – The destination type is larger than the source type. Type Conversion and Casting
  • 24. • It has this general form: – (target-type) value • if the size of the whole number • component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range. – Conversion of int to byte. – i and b 257 1
  • 25. • First, all byte and short values are promoted to int, as just described. • Then, if one operand is a long, the whole expression is promoted to long. • If one operand is a float, the entire expression is promoted to float. • If any of the operands is double, the result is double. type promotion rules
  • 26. • An array is a group of like-typed variables that are referred to by a common name. • Is a homogeneous data type Arrays
  • 27. • The general form of a one dimensional array declaration is – type array-var[ ]; • Here, type declares the base type of the array. • the value of array is set to null • new is a special operator that allocates memory. – array-var = new type[size]; One-Dimensional Arrays
  • 28. • First, you must declare a variable of the desired array type. • Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. • Thus, in Java all arrays are dynamically allocated.
  • 29. // Demonstrate a one-dimensional array. class Array { public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31; month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }
  • 30. • In Java, multidimensional arrays are actually arrays of arrays. • a two-dimensional array variable called twoD. – int twoD[][] = new int[4][5]; Multidimensional Arrays
  • 31.
  • 32. // Demonstrate a two-dimensional array. class TwoDArray { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
  • 33. • If you need only specify the memory for the first (leftmost) dimension. • You can allocate the remaining dimensions separately. – int twoD[][] = new int[4][]; – twoD[0] = new int[5]; – twoD[1] = new int[5]; – twoD[2] = new int[5]; – twoD[3] = new int[5];
  • 34. // Manually allocate differing size second dimensions. class TwoDAgain { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } } Output: 0 1 2 3 4 5 6 7 8 9
  • 35. – type[ ] var-name; • Eg: – int al[] = new int[3]; – int[] a2 = new int[3]; • The following declarations are also equivalent: – char twod1[][] = new char[3][4]; – char[][] twod2 = new char[3][4]; Alternative Array Declaration Syntax
  • 36. • Java does not support or allow pointers. • Java does not support pointers that can be accessed and/or modified by the programmer. • Java cannot allow pointers, because doing so would allow Java applets to breach the firewall between the Java execution environment and the host computer. Pointers in Java