SlideShare a Scribd company logo
basic core java up to operator
Chapter 1
Introduction
Java’s
Major Advantage
 over C & C++
Because pointers were a major source of
bugs in C and C++, Gosling omitted
pointers entirely from Java.

• Actually, pointers are still an important
part of the language--all objects are
referenced by pointers--but the language
handles them, not the programmer.
Java’s Origins in C & C++
Thus, it has been said that...

     “Java is C without the Guns and Knives.”
Java Architecture
• By now, Java itself has matured into its
3rd version, named Java 2. This course is
based on Java 2. The most current is Java
2 (1.5.1)
• Java is Object-Oriented--that means
everything in the language behaves like
an object.
• What exactly that means will be
explained in the coming weeks.
Java Architecture
Java’s Architecture comes from four separate
but intertwined technologies:
• the Java Programming Language
• the Java class file format
• the Java API, or Application Programming Interface
• the Java Virtual Machine
Java Architecture
Source programs are written in the Java Programming
Language.

All procedural code falls within methods.

Programs are compiled into Java class files.

Classes run in the Java Virtual Machine.
Java Architecture
• When a Java program runs, it is assisted by
other classes in the Java the Application
Programming Interface, or API.
Java Architecture
      Combined, the Java      Example Java API class files
    Virtual MachineObject.class
                     and the                String.class
         Java API form a
           “Platform.”
    Compile-Time
    Environment

     Hello.class

                                              Java
                                             Virtual
   Java Compiler
                                             Machine
                                            Run-Time Environment
    Hello.java
Java Architecture


• The Java Platform is unique, because it
can work without modification
     on any platform,
     on any operating system,
     if that platform has a
     “Java Virtual Machine.”
Java Architecture
                       Java
     What is the      Virtual      ?
                      Machine

Comparison of a typical Procedural
Program with a Java Program:
• In a typical C program, the source code is
compiled into a native machine language
module that consists of 1’s and 0’s.
C Source Code



           C object module
            compiled into
           machine language

• The machine language is specifically tailored
to one OS, be it Wintel, Mac, UNIX or MVS. •
Therefore, it is impossible for one object
module to be portable between platforms.
Java Architecture
         Java “bytecode”
In contrast to conventional programming
languages, a Java program is not compiled
into native machine language.
• Instead, Java makes bytecode.
• Bytecode is the result of a “compile”, but
the compile results in an intermediate form
that stops short of native machine-specific
code.
Java Architecture

• Instead of making a machine language
native code for each particular OS, Java
makes a single, universal bytecode module
that feeds into any Java Virtual Machine
(JVM).

• Each OS has its own different
implementation of the Java Virtual Machine.
Java Architecture

• The JVM sets up its own world within
your RAM.

• The JVM creates an internal
software-only sub-computer within the OS.

• The bytecode talks to the JVM, and the
JVM talks to the Operating System.
Java Architecture

• Thus, you get the Holy Grail of software reuse:



          “Write Once,
         Run Anywhere”.
Java Source

The            You can easily see why Bill
bytecode      Gates isn’t in love with Java!
is met
half-way
by the
JVM.


  JVM-Win      JVM-Mac     JVM-Unix     JVM-IBM
Java Architecture

  • The Virtual Machine interprets the
  bytecode one instruction at a time,
  and translates it into native machine
  code.

  • You compile your program once
  into bytecode, but it is interpreted
  anew every time it runs.
Security and the “Sandbox
C and C++ are famous for speed.

     • One reason they are fast is because C
and C++ don’t do things like checking the
bounds of arrays.                   • In C or
C++, a program can walk off the edge of an
array and invade the memory space beyond.
     • Hackers love that about C and C++.
Security and the “Sandbox”
• Another weakness of C/C++, that is a
favorite among Hackers, is the Buffer
Overflow.

• In this attack, the Hacker floods too much
data into a buffer and whatever overflows it
is turned loose on the system.

• Java solves these problems
Security and the “Sandbox”
• How Java Combats malicious code:
     Java checks array boundaries
     Java halts Buffer Overflows
     Java has Garbage collection to get
    rid of objects that are no longer
    used.
     Java’s compiler checks to make
    sure the code is safe before it runs.
• Gosling built security into Java, using a
    concept known as the “Sandbox.”
Security and the “Sandbox”
                   Local Code

    All Code, both Local and Remote, Must Pass Security Policy

         JDK 1.2                     Security Model

                                              SANDBOX




                               Vulnerable System Resources
                       (files, etc) Even Local Code is Not Trusted
Security and the “Sandbox”
• 5 Steps To Writing A Java Program:
     1.) Write it in a Text Editor
     2.) Compiler creates bytecode
     3.) The “Class loader” places the .
          class file in memory.
     4.) The “Bytecode Verifier” makes sure
     the code adheres to Java’s
        security rules.
     5.) The JVM Interpreter reads
        bytecode and makes platform
        native code.
Security and the “Sandbox”
• You see, preventing problems is a major
design consideration in Java.

• This idea led to the most import aspect of
Java: Object Orientation.

• Object Orientation protects data and lets a
program do only what is explicitly permitted.

• You could say Java is pessimistic.
Objects in Java

• In Java, Object Orientation is so
pervasive that it’s nearly impossible
to write a strictly procedural program
in the language.
Objects in Java

• Objects are reusable components.

• In Java, everything must be run from a
“class” file. This “class” contains bytecode.

• Java source code has the extension
Xxx.java
Objects in Java

• If I write a Java program called:

     Hello.java

     then, when compiled, this program will
be called:

     Hello.class
Objects in Java
• A class object is compiled Java code that
contains its own data variables, called
members, and sections of procedural code
called methods.

    If you have programmed in COBOL, a
method is like a paragraph you perform.

    If you have programmed in C or C++, a
method is like a function your program calls.
Objects in Java

• The combination of the data variables
         and the methods
                  that are used to read,
                                   write
                              or modify
                        those variables

is called a class.
Objects in Java

• Java has a rich collection of Class Libraries.

• These are also known as the Java API or
Application Programming Interface.

• To program in Java, you must

     i.) Learn the Language, and
     ii.) Learn the Class Libraries.
Objects in Java

• These class libraries greatly simplify your
job as a Java programmer.

• They help you to write complex
programs quickly.

• To master Java, you must master these
class libraries.
Compiling A Java Program
• You have created a Java program called
      Hello.java
• To compile it, you run the JDK supplied
utility called:
                javac
C:javac Hello.java
    If this was successful, a file called:
  Hello.class will be produced.
First Java Program
• The two largest varieties of Java
programs:

     Applications

     Applets
First Java Program

• A Java Application is a free-standing
program that is capable of running
directly in the Java Virtual Machine.

• A Java Applet is a mini-program that is
much more limited in its abilities. An
Applet can only run within the context of
an HTML browser.
First Java Program

• A Java Application is a free-standing
program that is capable of running
directly in the Java Virtual Machine.

• A Java Applet is a mini-program that is
much more limited in its abilities. An
Applet can only run within the context of
an HTML browser.
A Java Application
// HelloWorld.java Our first Java Application

public class HelloWorld
{
       public static void main( String args[])
       {
              System.out.println( “Hello World!” );
       }
}

Now our Application is complete. We have added the method “main”.
All methods are lower case. main is a special method--it actually runs
the program.
         In any application, you are always guaranteed that method
main will run.
C:>javac HelloWorld.java

  C:>


• A successful compile of your java
program will return to a bare cursor, as
you see here.
A Java Application
  C:>javac HelloWorld.java

  C:>java HelloWorld
  Hello World!

     • To run your compiled Application,
you enter lowercase java HelloWorld
on the command line.         • Notice, the
“.class” extension is omitted.
Now load the JDK1.4.1,
the documentation,
change the class path and
write your first Java program.
Chapter 2
Introduction to Java
    Applications
Java Applications Are
         A Series of Classes
• A Java Application must have the method
main.
• A Java Application begins executing at
main.
• Let’s look at details of an Application:
public class Welcome1
 {
        public static void main( String args[] )
        {
               System.out.println( “Welcome to Java!” );

        } // end of main()
 } // end of class Welcome1


• This is a basic Application.
• Notice the comments. These are required in this course.
Java is free form, but you’ll be happy if you get in the habit
of documenting like this.
• Also, whenever you type an opening curly bracket, type
the closing one right away.
• Your curly brackets must always--in this class--line up as
shown.
public class Welcome1
 {
        public static void main( String args[] )
        {
               System.out.println( “Welcome to Java!” );

        } // end of main()
 } // end of class Welcome1

• The line above in blue is the class definition for Welcome1.
• Every class name must be Capitalized.
• Notice, every scrap of code is within this class.
• Since it is named Welcome1, this Application is saved in a file
called Welcome1.java, spelled exactly the same.
• The compiler will make a file called Welcome1.class.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.println( “Welcome to Java!” );

       } // end of main()
} // end of class Welcome1


• The word Welcome1 is an identifier.
• An identifier is a user-defined word, which consists of:
       letters
       digits
       _ (underscore)
       $ (a dollar sign)
• An identifier cannot begin with a digit.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.println( “Welcome to Java!” );

       } // end of main()
} // end of class Welcome1


• Notice that we put the word public before the word
class.
• This means the class can be called by anything.
• The alternatives to public are discussed in Chapter 8.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.println( “Welcome to Java!” );

       } // end of main()
} // end of class Welcome1


• The method main is also declared public.
• This should just be copied until Chapter 6, when we
know methods better.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.println( “Welcome to Java!” );

       } // end of main()
} // end of class Welcome1


• void means nothing is returned to the operating
system when the program finishes.
• The ( String args[] ) works with “arguments”
that were passed when the program was executed.
• Although you cannot omit it ( String args[] ),
we don’t discuss this topic just yet, so please copy it.
public class Welcome1
  {
         public static void main( String args[] )
         {
                System.out.println( “Welcome to Java!” );

         } // end of main()
  } // end of class Welcome1

• The System.out.println puts the message in
quotes on the command console.
• If we used System.out.print, then the cursor
would not do a carriage return / line the after it prints the
                     This is called feed
text.                Standard output
                           object.
• Notice the opening and closing blue curly brackets. The
unit of code enclosed in them is called a “block.”
• It is also called the “body” of the method.
public class Welcome1
{
       public static void main( String args[] )
       {
             System.out.println( “Welcome to Java!” );

       } // end of main()
} // end of class Welcome1

 • You will find that you very rarely use this
 Standard output object.
 • Instead, you will use the GUI objects.

 • Notice in red the semicolon. ; All executable
 statements in Java ends in a semicolon.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.print( “Welcome ” );
              System.out.println( “to Java!” );

       } // end of main()
} // end of class Welcome1


• This will still produce the same text as the
previous version.
public class Welcome1
{
       public static void main( String args[] )
       {
              System.out.print( “WelcomentonJava! ” );
       } // end of main()
} // end of class Welcome1


• Notice the “ n ”. The slash is an escape
character. It tells the System object that whatever
follows the slash is special:
n    new line
      t tab
      r carriage return        Welcome                   to
                                                      Java!
       backslash
      ” quote
Primitive Data Types
• A variable called number1 actually refers to a place in
memory where the value of the variable is stored.
• Every variable in Java has a:
      name,
      type,
      size, and a
      value.
Primitive Data Types
      name

Variable names must conform to the rules for identifiers:

      • they must begin with a letter,

      • after that they can contain digits, dollar signs
       and underscores.

      • Java uses Unicode for its characters, so any
         “letter” that is valid for a word in any world
       language is therefore valid for a name in
      Java.
Primitive Data Types
      type
• The “type” appears before the identifier name.
• The type can be one of the “primitive data types” or it
can be any previously defined class.

    int num1;
                      • You declare a variable and initialize it on
                      the same line.
    num1 = 2;

                      • This is a declaration. At this point, the
                      name num1 refers to a location {a
                      pointer} in the computer’s RAM where
                      this variable is stored.
    int num1=2;
                      • Because an int is declared, we know
                      that four bytes are set aside.
                       • Still, nothing is stored in it yet.
Primitive Data Types
     size
       • When we assign a type [ int, String] to a
variable, we are not only declaring a memory
location.
      • We also decide how big of a number or
      character is able to be stored in that variable.
Primitive Data Types
    value
    • Finally, the value is what we want the
     variable to store.
Primitive Data Types
• Java is a Strongly-typed language. That
means, every variable must be declared as a type.
       In Java, there are 8 primitive types:

• 6 of those refer to numbers
       --4 for integers types,
       --2 for floating-point types,

• 1 is the character type char, used for characters
in Unicode encoding, and

• 1 is a boolean type for true or false
  values.
Primitive Data Types
    int
    • In contrast to C/C++, an int will always--no
    matter which operating system--take 4 bytes
    of storage space.
    • Because those 4 bytes are set in stone, you can be
            sure that every JVM that runs your program
    will be able to store the same size numbers.
     • int is the most commonly used number size.
    Range:
    -2,147,483,648 to 2,147,483,647 (over two billion)
Primitive Data Types
     short
      • In Java, a short is defined as 2 bytes, no
      matter which operating system is used.
       • You would only use this for special situations,
such as when speed is really crucial.
             { For VB programmers, a short is what
             you’ve come to think of as an int . }
      Range:
      -32,768 to 32,767
Primitive Data Types
     long
      • A long is defined as 8 bytes, no matter
which operating system is used.
      Range:
      -9,223,372,036,854,775,808L to


      9,223,372,036,854,775,807L
  • Hexadecimal numbers have a prefix: 0x
        0x1CFE.
  • Please notice the upper-case L suffix is appended to any
  long. This is required.
Primitive Data Types
 byte
  • A byte is defined as 1 byte, no matter
  which operating system is used.
  Range:
  -128 to 127


• Again, like a short, a byte is only used under
rare circumstances.
Primitive Data Types
 float
  • A float is defined as 4 bytes, no matter
  which operating system is used.
  Range:
  approximately 3.40282347E+38F
        ( 6-7 significant decimal digits )

• Because there are so few decimal places available,
float is not used all that often.
Primitive Data Types
    double
   • A double is defined as 8 bytes, no matter
         which operating system is used.
   Range:
   approximately 1.79769313486231570E+308
         ( 15 significant decimal digits )
   • “double is the one to have when you’re having more
   than one--decimal place, that is.”
   • This is the most common choice for any decimal.
   • double is the default, not float, therefore, no special
   character is appended. (See red arrow.)
Primitive Data Types
       char
       • A char is defined as 2 bytes, no matter which    operating
system is used. A char type always
     refers to a character in the Unicode encoding
     scheme. [uFFFF u is the escape character syntax]   About
65,536 different characters can be
     represented.
       • Single quotes denote a char constant

       „H‟    is a char constant

       “H”    is a string that happens to only contain
       a             single character.
Primitive Data Types
       char
       • A char is defined as 2 bytes. A char type is a         single
Unicode character.                                 [uFFFF u is the
escape character syntax--65,536     different characters can be
represented.]
       • Single quotes denote a single-letter char constant
       „H‟    is a char constant.
       “H” is a String that happens to only contain a
       single character--it is not a char.                      This
is a syntax error! The compiler will              complain.
Primitive Data Types
 boolean
 • A boolean type has only two values.
 • In contrast to C/C++, in Java 0 and 1 cannot
        stand in for true or false.
 • A boolean type must be assigned the value of
  the constants true or false.
      [Meaning, these exact lowercase words.]
Java Math Operators
• Addition         +
• Subtraction      -
• Multiplication   *
• Division         /
• Modulus          %
All are binary operators, i.e., they work with two
numbers. They are executed according to the rules
for operator precedence. [page 1240]
  (There is no operator for exponentiation in Java)
Java Math Operators

• Multiplication         *
• What happens if you multiply variables of different types?
  int x = 2;
  double y = 3.889, sum = 0.000;
  sum = y * x;



• The integer will be temporarily converted to a
double and two doubles will be multiplied.
• Afterwards, the original integer is unchanged.
Java Math Operators
• Rules for Temporary Conversions
1st Priority: If either of the operands is of type double, then the other one is converted to
double for the calculation.
2nd Priority: Otherwise, if either of the operands is of type float, then the other one is
converted to float for the calculation.
3rd Priority: Otherwise, if any of the operands is of type long, then the other one is
converted to long for the calculation.


Note: these conversions are automatic because none of them result in a loss of accuracy.
Java Math Operators
• Static Casts
So, what happens when you desire to convert a double to
a float? Information will inevitably be lost.
         • You accomplish this using a cast.
  int x = 2, sum = 0;
  double y = 3.889;
  sum = (int)y * x;
  { sum is now equal to 6 }



 • Here, a value of just 3 will be used for y.
 • If you want to round y, you a method from class
 Math:
             sum = (int)Math.round(y) * x;
Java Math Operators

• Division         /
• Division can lead to unexpected results:
If both operands are integers, then the result of the
division is also an integer.
Any fractional part of the division is discarded.

Therefore:          17/3 = 5
Java Math Operators

• Modulus                %
• The modulus operator is confusing at first, but
eventually it becomes your good friend.
In contrast to the division operator, it returns the
remainder of any division. The modulus operator can
only be used when both operands are integers.

      17 % 3 = 2
You say this “17 modulus 3 equals 2”
Comparison Operators
• These are used for selection
structures:
equality                 ==
not equal                !=
greater than             >
less than                <
greater than or equal    >=
less than or equal       <=
Comparison Operators

• The equality operator is a common source of mistakes:

equality                  ==
Note that two equal signs are always used.
The single equal sign     [      =     ] is only used for
assignment, that is, assigning the value on the right to the
variable on the left.
             num1 = 33;
Comparison Operators

• When you make a compound symbol using the equal
sign, the equal sign is always on the right:



equality                ==
not equal               !=
greater than or equal   >=
less than or equal      <=

More Related Content

What's hot (20)

PPTX
Java Programming
Elizabeth alexander
 
PPT
Core java slides
Abhilash Nair
 
PPTX
1 java programming- introduction
jyoti_lakhani
 
PPTX
1.introduction to java
Madhura Bhalerao
 
PPT
Java basics
suraj pandey
 
PPT
Fundamentals of JAVA
KUNAL GADHIA
 
PPTX
Programming in Java
Abhilash Nair
 
PPTX
Core java online training
Glory IT Technologies Pvt. Ltd.
 
PDF
Introduction to java (revised)
Sujit Majety
 
PDF
Java notes
Manish Swarnkar
 
PDF
Core java course syllabus
Papitha Velumani
 
PPT
Java for Recruiters
ph7 -
 
PPTX
Presentation on Core java
mahir jain
 
PPTX
Basics of JAVA programming
Elizabeth Thomas
 
PPT
Chapter 1 introduction to java technology
sshhzap
 
PPT
Java basic introduction
Ideal Eyes Business College
 
PDF
Introduction to Java Programming
Ravi Kant Sahu
 
PDF
Java unit 1
Shipra Swati
 
PPTX
JAVA PROGRAMMING
Niyitegekabilly
 
PDF
Java basics notes
poonguzhali1826
 
Java Programming
Elizabeth alexander
 
Core java slides
Abhilash Nair
 
1 java programming- introduction
jyoti_lakhani
 
1.introduction to java
Madhura Bhalerao
 
Java basics
suraj pandey
 
Fundamentals of JAVA
KUNAL GADHIA
 
Programming in Java
Abhilash Nair
 
Core java online training
Glory IT Technologies Pvt. Ltd.
 
Introduction to java (revised)
Sujit Majety
 
Java notes
Manish Swarnkar
 
Core java course syllabus
Papitha Velumani
 
Java for Recruiters
ph7 -
 
Presentation on Core java
mahir jain
 
Basics of JAVA programming
Elizabeth Thomas
 
Chapter 1 introduction to java technology
sshhzap
 
Java basic introduction
Ideal Eyes Business College
 
Introduction to Java Programming
Ravi Kant Sahu
 
Java unit 1
Shipra Swati
 
JAVA PROGRAMMING
Niyitegekabilly
 
Java basics notes
poonguzhali1826
 

Similar to basic core java up to operator (20)

PPTX
Java Programming Tutorials Basic to Advanced 1
JALALUDHEENVK1
 
PPT
Object Oriented Programming-JAVA
Home
 
PPTX
Programming in java ppt
MrsRLakshmiIT
 
PPTX
Programming in java ppt
MrsRBoomadeviIT
 
PDF
Java Programming
Prof. Dr. K. Adisesha
 
PDF
What is-java
Shahid Rasheed
 
PDF
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
projectfora2
 
PPTX
Java introduction
The icfai university jaipur
 
PPTX
1_Introduction to Java.pptx java programming
amitraj53904
 
PDF
Introduction to Java Programming.pdf
AdiseshaK
 
PPTX
Java chapter 1 basic introduction Unit-1.pptx
noosdysharma
 
PPTX
Java Programming (M&M)
mafffffe19
 
PPTX
OOP with Java
OmegaHub
 
PPT
Chapter 1 java
ahmed abugharsa
 
ODP
Introduction To Java.
Tushar Chauhan
 
DOCX
Srgoc java
Gaurav Singh
 
PPT
Java1
Lovepreet Kaur
 
PPT
Java1
denis diigula
 
DOCX
Introduction to java programming tutorial
jackschitze
 
Java Programming Tutorials Basic to Advanced 1
JALALUDHEENVK1
 
Object Oriented Programming-JAVA
Home
 
Programming in java ppt
MrsRLakshmiIT
 
Programming in java ppt
MrsRBoomadeviIT
 
Java Programming
Prof. Dr. K. Adisesha
 
What is-java
Shahid Rasheed
 
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
projectfora2
 
Java introduction
The icfai university jaipur
 
1_Introduction to Java.pptx java programming
amitraj53904
 
Introduction to Java Programming.pdf
AdiseshaK
 
Java chapter 1 basic introduction Unit-1.pptx
noosdysharma
 
Java Programming (M&M)
mafffffe19
 
OOP with Java
OmegaHub
 
Chapter 1 java
ahmed abugharsa
 
Introduction To Java.
Tushar Chauhan
 
Srgoc java
Gaurav Singh
 
Introduction to java programming tutorial
jackschitze
 
Ad

More from kamal kotecha (20)

PPS
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPT
Java servlet life cycle - methods ppt
kamal kotecha
 
PPS
Java rmi example program with code
kamal kotecha
 
PPS
Java rmi
kamal kotecha
 
PPS
Jdbc example program with access and MySql
kamal kotecha
 
PPS
Jdbc api
kamal kotecha
 
PPS
Jdbc architecture and driver types ppt
kamal kotecha
 
PPS
Java Exception handling
kamal kotecha
 
PPS
JSP Error handling
kamal kotecha
 
PPS
Jsp element
kamal kotecha
 
PPS
Jsp chapter 1
kamal kotecha
 
PPS
String and string buffer
kamal kotecha
 
PPS
Wrapper class
kamal kotecha
 
PPS
Packages and inbuilt classes of java
kamal kotecha
 
PPS
Interface
kamal kotecha
 
PPS
Inheritance chepter 7
kamal kotecha
 
PPS
Class method
kamal kotecha
 
PPS
Introduction to class in java
kamal kotecha
 
PPS
Control statements
kamal kotecha
 
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Network programming in java - PPT
kamal kotecha
 
Java servlet life cycle - methods ppt
kamal kotecha
 
Java rmi example program with code
kamal kotecha
 
Java rmi
kamal kotecha
 
Jdbc example program with access and MySql
kamal kotecha
 
Jdbc api
kamal kotecha
 
Jdbc architecture and driver types ppt
kamal kotecha
 
Java Exception handling
kamal kotecha
 
JSP Error handling
kamal kotecha
 
Jsp element
kamal kotecha
 
Jsp chapter 1
kamal kotecha
 
String and string buffer
kamal kotecha
 
Wrapper class
kamal kotecha
 
Packages and inbuilt classes of java
kamal kotecha
 
Interface
kamal kotecha
 
Inheritance chepter 7
kamal kotecha
 
Class method
kamal kotecha
 
Introduction to class in java
kamal kotecha
 
Control statements
kamal kotecha
 
Ad

Recently uploaded (20)

PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
infertility, types,causes, impact, and management
Ritu480198
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Difference between write and update in odoo 18
Celine George
 
Controller Request and Response in Odoo18
Celine George
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

basic core java up to operator

  • 4. Because pointers were a major source of bugs in C and C++, Gosling omitted pointers entirely from Java. • Actually, pointers are still an important part of the language--all objects are referenced by pointers--but the language handles them, not the programmer.
  • 5. Java’s Origins in C & C++ Thus, it has been said that... “Java is C without the Guns and Knives.”
  • 6. Java Architecture • By now, Java itself has matured into its 3rd version, named Java 2. This course is based on Java 2. The most current is Java 2 (1.5.1) • Java is Object-Oriented--that means everything in the language behaves like an object. • What exactly that means will be explained in the coming weeks.
  • 7. Java Architecture Java’s Architecture comes from four separate but intertwined technologies: • the Java Programming Language • the Java class file format • the Java API, or Application Programming Interface • the Java Virtual Machine
  • 8. Java Architecture Source programs are written in the Java Programming Language. All procedural code falls within methods. Programs are compiled into Java class files. Classes run in the Java Virtual Machine.
  • 9. Java Architecture • When a Java program runs, it is assisted by other classes in the Java the Application Programming Interface, or API.
  • 10. Java Architecture Combined, the Java Example Java API class files Virtual MachineObject.class and the String.class Java API form a “Platform.” Compile-Time Environment Hello.class Java Virtual Java Compiler Machine Run-Time Environment Hello.java
  • 11. Java Architecture • The Java Platform is unique, because it can work without modification on any platform, on any operating system, if that platform has a “Java Virtual Machine.”
  • 12. Java Architecture Java What is the Virtual ? Machine Comparison of a typical Procedural Program with a Java Program: • In a typical C program, the source code is compiled into a native machine language module that consists of 1’s and 0’s.
  • 13. C Source Code C object module compiled into machine language • The machine language is specifically tailored to one OS, be it Wintel, Mac, UNIX or MVS. • Therefore, it is impossible for one object module to be portable between platforms.
  • 14. Java Architecture Java “bytecode” In contrast to conventional programming languages, a Java program is not compiled into native machine language. • Instead, Java makes bytecode. • Bytecode is the result of a “compile”, but the compile results in an intermediate form that stops short of native machine-specific code.
  • 15. Java Architecture • Instead of making a machine language native code for each particular OS, Java makes a single, universal bytecode module that feeds into any Java Virtual Machine (JVM). • Each OS has its own different implementation of the Java Virtual Machine.
  • 16. Java Architecture • The JVM sets up its own world within your RAM. • The JVM creates an internal software-only sub-computer within the OS. • The bytecode talks to the JVM, and the JVM talks to the Operating System.
  • 17. Java Architecture • Thus, you get the Holy Grail of software reuse: “Write Once, Run Anywhere”.
  • 18. Java Source The You can easily see why Bill bytecode Gates isn’t in love with Java! is met half-way by the JVM. JVM-Win JVM-Mac JVM-Unix JVM-IBM
  • 19. Java Architecture • The Virtual Machine interprets the bytecode one instruction at a time, and translates it into native machine code. • You compile your program once into bytecode, but it is interpreted anew every time it runs.
  • 20. Security and the “Sandbox
  • 21. C and C++ are famous for speed. • One reason they are fast is because C and C++ don’t do things like checking the bounds of arrays. • In C or C++, a program can walk off the edge of an array and invade the memory space beyond. • Hackers love that about C and C++.
  • 22. Security and the “Sandbox” • Another weakness of C/C++, that is a favorite among Hackers, is the Buffer Overflow. • In this attack, the Hacker floods too much data into a buffer and whatever overflows it is turned loose on the system. • Java solves these problems
  • 23. Security and the “Sandbox” • How Java Combats malicious code: Java checks array boundaries Java halts Buffer Overflows Java has Garbage collection to get rid of objects that are no longer used. Java’s compiler checks to make sure the code is safe before it runs. • Gosling built security into Java, using a concept known as the “Sandbox.”
  • 24. Security and the “Sandbox” Local Code All Code, both Local and Remote, Must Pass Security Policy JDK 1.2 Security Model SANDBOX Vulnerable System Resources (files, etc) Even Local Code is Not Trusted
  • 25. Security and the “Sandbox” • 5 Steps To Writing A Java Program: 1.) Write it in a Text Editor 2.) Compiler creates bytecode 3.) The “Class loader” places the . class file in memory. 4.) The “Bytecode Verifier” makes sure the code adheres to Java’s security rules. 5.) The JVM Interpreter reads bytecode and makes platform native code.
  • 26. Security and the “Sandbox” • You see, preventing problems is a major design consideration in Java. • This idea led to the most import aspect of Java: Object Orientation. • Object Orientation protects data and lets a program do only what is explicitly permitted. • You could say Java is pessimistic.
  • 27. Objects in Java • In Java, Object Orientation is so pervasive that it’s nearly impossible to write a strictly procedural program in the language.
  • 28. Objects in Java • Objects are reusable components. • In Java, everything must be run from a “class” file. This “class” contains bytecode. • Java source code has the extension Xxx.java
  • 29. Objects in Java • If I write a Java program called: Hello.java then, when compiled, this program will be called: Hello.class
  • 30. Objects in Java • A class object is compiled Java code that contains its own data variables, called members, and sections of procedural code called methods. If you have programmed in COBOL, a method is like a paragraph you perform. If you have programmed in C or C++, a method is like a function your program calls.
  • 31. Objects in Java • The combination of the data variables and the methods that are used to read, write or modify those variables is called a class.
  • 32. Objects in Java • Java has a rich collection of Class Libraries. • These are also known as the Java API or Application Programming Interface. • To program in Java, you must i.) Learn the Language, and ii.) Learn the Class Libraries.
  • 33. Objects in Java • These class libraries greatly simplify your job as a Java programmer. • They help you to write complex programs quickly. • To master Java, you must master these class libraries.
  • 34. Compiling A Java Program • You have created a Java program called Hello.java • To compile it, you run the JDK supplied utility called: javac C:javac Hello.java If this was successful, a file called: Hello.class will be produced.
  • 35. First Java Program • The two largest varieties of Java programs: Applications Applets
  • 36. First Java Program • A Java Application is a free-standing program that is capable of running directly in the Java Virtual Machine. • A Java Applet is a mini-program that is much more limited in its abilities. An Applet can only run within the context of an HTML browser.
  • 37. First Java Program • A Java Application is a free-standing program that is capable of running directly in the Java Virtual Machine. • A Java Applet is a mini-program that is much more limited in its abilities. An Applet can only run within the context of an HTML browser.
  • 38. A Java Application // HelloWorld.java Our first Java Application public class HelloWorld { public static void main( String args[]) { System.out.println( “Hello World!” ); } } Now our Application is complete. We have added the method “main”. All methods are lower case. main is a special method--it actually runs the program. In any application, you are always guaranteed that method main will run.
  • 39. C:>javac HelloWorld.java C:> • A successful compile of your java program will return to a bare cursor, as you see here.
  • 40. A Java Application C:>javac HelloWorld.java C:>java HelloWorld Hello World! • To run your compiled Application, you enter lowercase java HelloWorld on the command line. • Notice, the “.class” extension is omitted.
  • 41. Now load the JDK1.4.1, the documentation, change the class path and write your first Java program.
  • 42. Chapter 2 Introduction to Java Applications
  • 43. Java Applications Are A Series of Classes • A Java Application must have the method main. • A Java Application begins executing at main. • Let’s look at details of an Application:
  • 44. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • This is a basic Application. • Notice the comments. These are required in this course. Java is free form, but you’ll be happy if you get in the habit of documenting like this. • Also, whenever you type an opening curly bracket, type the closing one right away. • Your curly brackets must always--in this class--line up as shown.
  • 45. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • The line above in blue is the class definition for Welcome1. • Every class name must be Capitalized. • Notice, every scrap of code is within this class. • Since it is named Welcome1, this Application is saved in a file called Welcome1.java, spelled exactly the same. • The compiler will make a file called Welcome1.class.
  • 46. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • The word Welcome1 is an identifier. • An identifier is a user-defined word, which consists of: letters digits _ (underscore) $ (a dollar sign) • An identifier cannot begin with a digit.
  • 47. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • Notice that we put the word public before the word class. • This means the class can be called by anything. • The alternatives to public are discussed in Chapter 8.
  • 48. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • The method main is also declared public. • This should just be copied until Chapter 6, when we know methods better.
  • 49. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • void means nothing is returned to the operating system when the program finishes. • The ( String args[] ) works with “arguments” that were passed when the program was executed. • Although you cannot omit it ( String args[] ), we don’t discuss this topic just yet, so please copy it.
  • 50. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • The System.out.println puts the message in quotes on the command console. • If we used System.out.print, then the cursor would not do a carriage return / line the after it prints the This is called feed text. Standard output object. • Notice the opening and closing blue curly brackets. The unit of code enclosed in them is called a “block.” • It is also called the “body” of the method.
  • 51. public class Welcome1 { public static void main( String args[] ) { System.out.println( “Welcome to Java!” ); } // end of main() } // end of class Welcome1 • You will find that you very rarely use this Standard output object. • Instead, you will use the GUI objects. • Notice in red the semicolon. ; All executable statements in Java ends in a semicolon.
  • 52. public class Welcome1 { public static void main( String args[] ) { System.out.print( “Welcome ” ); System.out.println( “to Java!” ); } // end of main() } // end of class Welcome1 • This will still produce the same text as the previous version.
  • 53. public class Welcome1 { public static void main( String args[] ) { System.out.print( “WelcomentonJava! ” ); } // end of main() } // end of class Welcome1 • Notice the “ n ”. The slash is an escape character. It tells the System object that whatever follows the slash is special: n new line t tab r carriage return Welcome to Java! backslash ” quote
  • 54. Primitive Data Types • A variable called number1 actually refers to a place in memory where the value of the variable is stored. • Every variable in Java has a: name, type, size, and a value.
  • 55. Primitive Data Types name Variable names must conform to the rules for identifiers: • they must begin with a letter, • after that they can contain digits, dollar signs and underscores. • Java uses Unicode for its characters, so any “letter” that is valid for a word in any world language is therefore valid for a name in Java.
  • 56. Primitive Data Types type • The “type” appears before the identifier name. • The type can be one of the “primitive data types” or it can be any previously defined class. int num1; • You declare a variable and initialize it on the same line. num1 = 2; • This is a declaration. At this point, the name num1 refers to a location {a pointer} in the computer’s RAM where this variable is stored. int num1=2; • Because an int is declared, we know that four bytes are set aside. • Still, nothing is stored in it yet.
  • 57. Primitive Data Types size • When we assign a type [ int, String] to a variable, we are not only declaring a memory location. • We also decide how big of a number or character is able to be stored in that variable.
  • 58. Primitive Data Types value • Finally, the value is what we want the variable to store.
  • 59. Primitive Data Types • Java is a Strongly-typed language. That means, every variable must be declared as a type. In Java, there are 8 primitive types: • 6 of those refer to numbers --4 for integers types, --2 for floating-point types, • 1 is the character type char, used for characters in Unicode encoding, and • 1 is a boolean type for true or false values.
  • 60. Primitive Data Types int • In contrast to C/C++, an int will always--no matter which operating system--take 4 bytes of storage space. • Because those 4 bytes are set in stone, you can be sure that every JVM that runs your program will be able to store the same size numbers. • int is the most commonly used number size. Range: -2,147,483,648 to 2,147,483,647 (over two billion)
  • 61. Primitive Data Types short • In Java, a short is defined as 2 bytes, no matter which operating system is used. • You would only use this for special situations, such as when speed is really crucial. { For VB programmers, a short is what you’ve come to think of as an int . } Range: -32,768 to 32,767
  • 62. Primitive Data Types long • A long is defined as 8 bytes, no matter which operating system is used. Range: -9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L • Hexadecimal numbers have a prefix: 0x 0x1CFE. • Please notice the upper-case L suffix is appended to any long. This is required.
  • 63. Primitive Data Types byte • A byte is defined as 1 byte, no matter which operating system is used. Range: -128 to 127 • Again, like a short, a byte is only used under rare circumstances.
  • 64. Primitive Data Types float • A float is defined as 4 bytes, no matter which operating system is used. Range: approximately 3.40282347E+38F ( 6-7 significant decimal digits ) • Because there are so few decimal places available, float is not used all that often.
  • 65. Primitive Data Types double • A double is defined as 8 bytes, no matter which operating system is used. Range: approximately 1.79769313486231570E+308 ( 15 significant decimal digits ) • “double is the one to have when you’re having more than one--decimal place, that is.” • This is the most common choice for any decimal. • double is the default, not float, therefore, no special character is appended. (See red arrow.)
  • 66. Primitive Data Types char • A char is defined as 2 bytes, no matter which operating system is used. A char type always refers to a character in the Unicode encoding scheme. [uFFFF u is the escape character syntax] About 65,536 different characters can be represented. • Single quotes denote a char constant „H‟ is a char constant “H” is a string that happens to only contain a single character.
  • 67. Primitive Data Types char • A char is defined as 2 bytes. A char type is a single Unicode character. [uFFFF u is the escape character syntax--65,536 different characters can be represented.] • Single quotes denote a single-letter char constant „H‟ is a char constant. “H” is a String that happens to only contain a single character--it is not a char. This is a syntax error! The compiler will complain.
  • 68. Primitive Data Types boolean • A boolean type has only two values. • In contrast to C/C++, in Java 0 and 1 cannot stand in for true or false. • A boolean type must be assigned the value of the constants true or false. [Meaning, these exact lowercase words.]
  • 69. Java Math Operators • Addition + • Subtraction - • Multiplication * • Division / • Modulus % All are binary operators, i.e., they work with two numbers. They are executed according to the rules for operator precedence. [page 1240] (There is no operator for exponentiation in Java)
  • 70. Java Math Operators • Multiplication * • What happens if you multiply variables of different types? int x = 2; double y = 3.889, sum = 0.000; sum = y * x; • The integer will be temporarily converted to a double and two doubles will be multiplied. • Afterwards, the original integer is unchanged.
  • 71. Java Math Operators • Rules for Temporary Conversions 1st Priority: If either of the operands is of type double, then the other one is converted to double for the calculation. 2nd Priority: Otherwise, if either of the operands is of type float, then the other one is converted to float for the calculation. 3rd Priority: Otherwise, if any of the operands is of type long, then the other one is converted to long for the calculation. Note: these conversions are automatic because none of them result in a loss of accuracy.
  • 72. Java Math Operators • Static Casts So, what happens when you desire to convert a double to a float? Information will inevitably be lost. • You accomplish this using a cast. int x = 2, sum = 0; double y = 3.889; sum = (int)y * x; { sum is now equal to 6 } • Here, a value of just 3 will be used for y. • If you want to round y, you a method from class Math: sum = (int)Math.round(y) * x;
  • 73. Java Math Operators • Division / • Division can lead to unexpected results: If both operands are integers, then the result of the division is also an integer. Any fractional part of the division is discarded. Therefore: 17/3 = 5
  • 74. Java Math Operators • Modulus % • The modulus operator is confusing at first, but eventually it becomes your good friend. In contrast to the division operator, it returns the remainder of any division. The modulus operator can only be used when both operands are integers. 17 % 3 = 2 You say this “17 modulus 3 equals 2”
  • 75. Comparison Operators • These are used for selection structures: equality == not equal != greater than > less than < greater than or equal >= less than or equal <=
  • 76. Comparison Operators • The equality operator is a common source of mistakes: equality == Note that two equal signs are always used. The single equal sign [ = ] is only used for assignment, that is, assigning the value on the right to the variable on the left. num1 = 33;
  • 77. Comparison Operators • When you make a compound symbol using the equal sign, the equal sign is always on the right: equality == not equal != greater than or equal >= less than or equal <=