SlideShare a Scribd company logo
Java in two semesters by Quentin Chratan and Aaron Kans
 public class Hello
    Object Oriented languages require that program is to be
     written in separate units called classes
    Telling the compiler that we are writing a class named
     as Hello
    Public indicates that we are placing this class accessible
     to every one
    A public class should always be saved in file with the
     same name as of the class
    So what will be name of our program??
       Hello.java
    Our program will interact with multiple built in java
     libraries classes
{}
    Everything in class has to be contained between two curly
     brackets (Braces) to indicate the beginning and end of class
 Java is case sensitive
    Upper case letters and lower case letters are different
 public static void main(String[] args)
    Every program will have at least one class with this line
    This is method
    Main is a special method, this is where the program begins
    A program starts with the first instruction of main and then
     obeys each instruction in sequence.
    The program terminates when it has finished obeying the final
     instruction of main
    { } indicates the body of main and marks its starting and
     ending
 System.out.println(“Hello World”);
      Display “Hello World”
      println is short for “print line”
;
      Java instruction has to end with a semi colon.
 Although you can choose any name such as x, its best to pick a
  name that describe the purpose of data item
 E.g.
    A computer game might need a piece of data to record the
     player’s score as secret key.
    What should be the name of variable to store such data
        Score
    What should be the data type of such variable (byte, short, int
     and long)
        int
 You can choose any name for variable as long as
    The name is not already a word in java language (class, void,
     public)
    Name has no spaces in it
    Name does not include operators or mathematical symbols such
     as + or –
    Name starts with either a letter, an underscore (_) or a dollar sign
     ($)
    You can use any letter as starting letter but java convention is to
     begin the name with lower case letter
Computer Memory         Java Instruction


                  int score; hits;
                      score,



                  char level;
 Allows values to be put into variables
 Written with equality symbol (=)
    Assignment Operator
 variableName = value;
    score =0;
    Set value of score to zero OR score becomes equal to zero
    Puts the number zero into memory we called score
 Combine variable statement with a variable declaration
        int score = 0;        int score;
                              score =0;
 What will be the effect of following statement in java?
    int score = 2.5
    This statement will not compile as 2.5 is a real number
 double something =1000;
   Though 1000 is an integer but this statement is legal as
    will not result in information loss. It will be considered
    as 1000.0
 Character Assignment
   char level = ‘A’;
      Enclosed he character in single quotes
      Assignment at the time of declaration
   level =‘B’
      Assignments changed
 Data items in program have values that do not change
 Following are examples of such items
   Maximum score in a exam
   Number of hours in a day
   Mathematical value of  (3.14176)
 In these cases the values do not vary
 Values remain constant
 Such items should be named and declared as
  constants
 Add the keyword “final” before declaration
   final int HOURS = 24
 The statement HOURS = 12 will not compile now
Operation        Java Operator
Addition         +
Subtraction      -
Multiplication   *
Division         /
Remainder        %
int x;
x =10 + 25;
double cost;
cost = 500*(1+17.5/100);
int x;
x=30/4;
What Will be the answer? 7 or 7.5?

The answer is 7 as division operator is overloaded
double price, tax, cost;
price = 500;
tax =17.5;
cost =price+(1+tax/100);
price =price+(1+tax/100);
 X = X + 1;
    X++;
 X = X – 1;
    X--;
 int X = 5;
 int Y = 0;
      Postfix             Prefix
      Y=X--;
      Y=X++;              Y=--X;
                          Y=++X;
      Y value will be 5   Y value will be 6
                                          4
      X value will be 6
                      4   X value will be 6
                                          4


 •Y = Y + X;
 •Y + = X;
Comp102   lec 4
Comp102   lec 4
 System.out.println(“Hello World”);
 System.out.println(“Welcome to java world”);
 Hello World
 Welcome to java world

 System.out.print(“Hello World”);
 System.out.println(“Welcome to java world”);
 Hello WorldWelcome to java world

 System.out.println();
 Blank line in the program
 Strings
    Collection of characters
    Always enclosed in speech marks “ “
    Print statements print strings
    Several strings can be combined using + operator
       Concatenation Operator (+)
    System.out.println(“Hello” + “World”);
       HelloWorld
    Spaces included in speech marks are printed
       System.out.println(“Hello ” + “World”);
             Hello World
 System.out.println(10*10);
    The instruction prints 100 on screen.
    Java converts value/expression to a string before displaying it
    As these numbers are converted into string so they can be
     combined
    System.out.println(“Cost = ”+(10*10));
       Cost = 100
 Complex data type
    Name
    Address
    Car Registration Number
    Any meaningless sequence of characters
 Declare it in the same was as declare variables
    String Name;
    Name = “Saira”;
   OR
    String Name = “Saira”;
 Part of Java release 5.0 and later
 Class that makes it easy for us to write a program
  that obtains information that is typed in at the
  keyboard
 Scanner is part of Java package called util
    A package is a collection of pre-compiled classes
 To make Scanner class accessible to compiler, we
  have to tell the compiler that it should look in util
  package
    import java.util.*;
       * means all classes in util package are made available
    import java.util.Scanner;
       Only Scanner class is accessible
 Create Object
    Scanner sc = new Scanner(System.in);
       System.in represent KeyBoard
       new instantiates a class by allocating memory for a new object
        and returning a reference to that memory.
 Integer Input
    int x;
    x=sc.nextInt();
 Double Input
    double x;
    x=sc.nextDouble();
 String Input
    String x;
    x=sc.next(); OR x=sc.nextLine();
 Character Input
    char x;
    x=sc.next().charAt(0);

More Related Content

PDF
Learn Java Part 2
PPSX
DITEC - Programming with C#.NET
PPTX
Programming Primer EncapsulationVB
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PDF
Implicit conversion and parameters
PPT
Scala functions
PPSX
Esoft Metro Campus - Certificate in java basics
PPSX
DIWE - Advanced PHP Concepts
Learn Java Part 2
DITEC - Programming with C#.NET
Programming Primer EncapsulationVB
Esoft Metro Campus - Certificate in c / c++ programming
Implicit conversion and parameters
Scala functions
Esoft Metro Campus - Certificate in java basics
DIWE - Advanced PHP Concepts

What's hot (20)

PPTX
Python functions part12
PPTX
Object oreinted php | OOPs
PDF
Applicative Functor - Part 2
PPS
Procedures functions structures in VB.Net
PPSX
Esoft Metro Campus - Programming with C++
PPTX
Perl slid
PPSX
DISE - Windows Based Application Development in C#
PPTX
Python programming: Anonymous functions, String operations
PPTX
Python: Basic Inheritance
PDF
Functions
PPT
01 c++ Intro.ppt
PPSX
DISE - Windows Based Application Development in Java
PPTX
Python Programming Essentials - M17 - Functions
PPTX
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
PDF
Python Programming - II. The Basics
PPTX
Advance python programming
PPTX
Chapter 02 functions -class xii
PPTX
OOPS Basics With Example
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Python functions part12
Object oreinted php | OOPs
Applicative Functor - Part 2
Procedures functions structures in VB.Net
Esoft Metro Campus - Programming with C++
Perl slid
DISE - Windows Based Application Development in C#
Python programming: Anonymous functions, String operations
Python: Basic Inheritance
Functions
01 c++ Intro.ppt
DISE - Windows Based Application Development in Java
Python Programming Essentials - M17 - Functions
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Python Programming - II. The Basics
Advance python programming
Chapter 02 functions -class xii
OOPS Basics With Example
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Ad

Viewers also liked (8)

PPT
Comp102 lec 9
PPT
Comp102 lec 3
PPT
Comp102 lec 5.1
PPT
Comp102 lec 5.0
PPT
Comp102 lec 8
PPT
Comp102 lec 10
PPT
Comp102 lec 7
PPTX
Case studys of RockSound and Kerrang!
Comp102 lec 9
Comp102 lec 3
Comp102 lec 5.1
Comp102 lec 5.0
Comp102 lec 8
Comp102 lec 10
Comp102 lec 7
Case studys of RockSound and Kerrang!
Ad

Similar to Comp102 lec 4 (20)

PPTX
Java fundamentals
PPTX
Lecture2_MCS4_Evening.pptx
PPT
Basic elements of java
PPT
ch02-primitive-data-definite-loops.ppt
PPT
ch02-primitive-data-definite-loops.ppt
PPT
Unit I Advanced Java Programming Course
PPTX
JAVA programming language made easy.pptx
PPT
Ap Power Point Chpt2
PPT
Ch02 primitive-data-definite-loops
PPT
Chap02
PDF
Starting Out with Java From Control Structures through Objects 6th Edition Ga...
PPTX
Java introduction
PPTX
Basics of java 2
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
PPT
Java basic tutorial by sanjeevini india
PPT
Java basic tutorial by sanjeevini india
PPTX
Object oriented programming1 Week 1.pptx
PPTX
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx
Java fundamentals
Lecture2_MCS4_Evening.pptx
Basic elements of java
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
Unit I Advanced Java Programming Course
JAVA programming language made easy.pptx
Ap Power Point Chpt2
Ch02 primitive-data-definite-loops
Chap02
Starting Out with Java From Control Structures through Objects 6th Edition Ga...
Java introduction
Basics of java 2
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Object oriented programming1 Week 1.pptx
Introduction to JcjfjfjfkuutyuyrsdterdfbvAVA.pptx

Comp102 lec 4

  • 1. Java in two semesters by Quentin Chratan and Aaron Kans
  • 2.  public class Hello  Object Oriented languages require that program is to be written in separate units called classes  Telling the compiler that we are writing a class named as Hello  Public indicates that we are placing this class accessible to every one  A public class should always be saved in file with the same name as of the class  So what will be name of our program??  Hello.java  Our program will interact with multiple built in java libraries classes
  • 3. {}  Everything in class has to be contained between two curly brackets (Braces) to indicate the beginning and end of class  Java is case sensitive  Upper case letters and lower case letters are different  public static void main(String[] args)  Every program will have at least one class with this line  This is method  Main is a special method, this is where the program begins  A program starts with the first instruction of main and then obeys each instruction in sequence.  The program terminates when it has finished obeying the final instruction of main  { } indicates the body of main and marks its starting and ending
  • 4.  System.out.println(“Hello World”);  Display “Hello World”  println is short for “print line” ;  Java instruction has to end with a semi colon.
  • 5.  Although you can choose any name such as x, its best to pick a name that describe the purpose of data item  E.g.  A computer game might need a piece of data to record the player’s score as secret key.  What should be the name of variable to store such data  Score  What should be the data type of such variable (byte, short, int and long)  int  You can choose any name for variable as long as  The name is not already a word in java language (class, void, public)  Name has no spaces in it  Name does not include operators or mathematical symbols such as + or –  Name starts with either a letter, an underscore (_) or a dollar sign ($)  You can use any letter as starting letter but java convention is to begin the name with lower case letter
  • 6. Computer Memory Java Instruction int score; hits; score, char level;
  • 7.  Allows values to be put into variables  Written with equality symbol (=)  Assignment Operator  variableName = value;  score =0;  Set value of score to zero OR score becomes equal to zero  Puts the number zero into memory we called score  Combine variable statement with a variable declaration int score = 0; int score; score =0;  What will be the effect of following statement in java?  int score = 2.5  This statement will not compile as 2.5 is a real number
  • 8.  double something =1000;  Though 1000 is an integer but this statement is legal as will not result in information loss. It will be considered as 1000.0  Character Assignment  char level = ‘A’;  Enclosed he character in single quotes  Assignment at the time of declaration  level =‘B’  Assignments changed
  • 9.  Data items in program have values that do not change  Following are examples of such items  Maximum score in a exam  Number of hours in a day  Mathematical value of  (3.14176)  In these cases the values do not vary  Values remain constant  Such items should be named and declared as constants  Add the keyword “final” before declaration  final int HOURS = 24  The statement HOURS = 12 will not compile now
  • 10. Operation Java Operator Addition + Subtraction - Multiplication * Division / Remainder %
  • 11. int x; x =10 + 25; double cost; cost = 500*(1+17.5/100); int x; x=30/4; What Will be the answer? 7 or 7.5? The answer is 7 as division operator is overloaded double price, tax, cost; price = 500; tax =17.5; cost =price+(1+tax/100); price =price+(1+tax/100);
  • 12.  X = X + 1;  X++;  X = X – 1;  X--;  int X = 5;  int Y = 0; Postfix Prefix Y=X--; Y=X++; Y=--X; Y=++X; Y value will be 5 Y value will be 6 4 X value will be 6 4 X value will be 6 4 •Y = Y + X; •Y + = X;
  • 15.  System.out.println(“Hello World”);  System.out.println(“Welcome to java world”); Hello World Welcome to java world  System.out.print(“Hello World”);  System.out.println(“Welcome to java world”); Hello WorldWelcome to java world  System.out.println(); Blank line in the program
  • 16.  Strings  Collection of characters  Always enclosed in speech marks “ “  Print statements print strings  Several strings can be combined using + operator  Concatenation Operator (+)  System.out.println(“Hello” + “World”);  HelloWorld  Spaces included in speech marks are printed  System.out.println(“Hello ” + “World”);  Hello World  System.out.println(10*10);  The instruction prints 100 on screen.  Java converts value/expression to a string before displaying it  As these numbers are converted into string so they can be combined  System.out.println(“Cost = ”+(10*10));  Cost = 100
  • 17.  Complex data type  Name  Address  Car Registration Number  Any meaningless sequence of characters  Declare it in the same was as declare variables  String Name;  Name = “Saira”; OR  String Name = “Saira”;
  • 18.  Part of Java release 5.0 and later  Class that makes it easy for us to write a program that obtains information that is typed in at the keyboard  Scanner is part of Java package called util  A package is a collection of pre-compiled classes  To make Scanner class accessible to compiler, we have to tell the compiler that it should look in util package  import java.util.*;  * means all classes in util package are made available  import java.util.Scanner;  Only Scanner class is accessible
  • 19.  Create Object  Scanner sc = new Scanner(System.in);  System.in represent KeyBoard  new instantiates a class by allocating memory for a new object and returning a reference to that memory.  Integer Input  int x;  x=sc.nextInt();  Double Input  double x;  x=sc.nextDouble();  String Input  String x;  x=sc.next(); OR x=sc.nextLine();  Character Input  char x;  x=sc.next().charAt(0);