Overview

1 / 17
About This Presentation
Title:

Overview

Description:

Comment. CS110J. Variables. Declaration consists of: variable type variable name ... Good for long comment blocks. CS110J. Example ... –

Number of Views:44
Avg rating:3.0/5.0
Slides: 18
Provided by: cs11
Category:
Tags: comment | overview

less

Transcript and Presenter's Notes

Title: Overview


1
Overview
  • Recap - Introduction to Java
  • Your first Java program
  • Variables
  • Data Types
  • Operators and Expressions
  • Simple Input/Output

2
Write Once, Run Anywhere
Java Code (High Level)
Byte Code
JVM for Machine 1
Machine 1
Translates BC to ML for Machine 1
Java Compiler
JVM for Machine 2
Machine 2
Translates Java Code to Byte Code
Translates BC to ML for Machine 2
3
Compilation and Execution
javac - The Java compiler javac
HelloWorld.javaproduces HelloWorld.class java -
The JVM java HelloWorldruns HelloWorld.class in
the JVM.
4
First Java Program (HelloWorld)
  • Start up a text editor (xemacs, pico, vi)
  • Type in the followingpublic class
    HelloWorld public static void main(String
    args) //printing to the screen System.out.p
    rintln(Hello World!)
  • Save it as a file named HelloWorld.java

5
HelloWorld (contd)
  • Compile the program with
  • javac HelloWorld.java
  • Run the program with
  • java HelloWorld

6
Analysis of HelloWorld.java
Class name
public class HelloWorld public static void
main(String args) // printing to the
screen System.out.println(Hello
World!)
Main class
Main method (entry point)
Comment
Function call
Function name
String to be printed
7
Variables
  • Declaration consists ofltvariable typegt
    ltvariable namegt
  • The name may
  • consist of letters, digits, and _
  • not start with a digit
  • The type can be a primitive data type or an
    Object data type.

8
Primitive Data Types
  • boolean - true/false
  • char - letters/digits
  • byte - an 8-bit value
  • short, int, long - signed integers with 16, 32,
    and 64 bits of precision, respectively
  • float, double - IEEE floating point numbers
    (decimal numbers) with 32 and 64 bits of
    precision, for about 7 and 14 significant digits,
    respectively.

9
Example Variable Declarations
  • E.g. boolean isHappy int counter double pi
  • The variable can be initialized along with its
    declaration boolean isHappy
    true int counter 0 double pi 3.1428

10
Object Data Types
  • Java includes a large class library
  • Classes - to be discussed later
  • String a simple example of a class
  • Represents a sequence of letters/digits, etcex.
    String aStringVariable Some letters
  • Inside a string, use escape codes \t - tab \n
    - new line\\ - a backslash character\ - a
    quote character

11
Operators
12
Basic Operations
  • int quizPercent 100int mpPercent
    65double finalGrade quizPercent0.40
    mpPercent0.60String firstName
    SteveString lastName ZelinkaString
    fullName firstName lastNameString
    report fullName received finalGrade

13
Output
  • Output a string
  • System.out.print(lta Stringgt)
  • Output a string followed by a newline
  • System.out.println(lta Stringgt)
  • E.g.
  • System.out.println(report)

14
Input
  • Input a string
  • BufferedReader reader new BufferedReader(new
    InputStreamReader(System.in))String inputString
    reader.readLine()
  • Converting input Strings
  • int anInteger Integer.parseInt(inputString)
  • Similar constructions for other numeric data
    typesdouble doublesToo Double.parseDouble(input
    String)short andShorts Short.parseShort(inputS
    tring)etc.

15
Comments
  • Two kinds // or / /
  • //
  • Everything up to the next line is ignored
  • Good for short comments
  • / /
  • Everything between is ignored / ends the
    comment.
  • Good for long comment blocks.

16
Example
  • / This code receives two integers as input,
    calculates their sum, and prints the result on
    the screen. /String inputBufferedReader
    reader new BufferedReader(new
    InputStreamReader(System.in))System.out.print(E
    nter the first number )input
    reader.readLine()int theFirst
    Integer.parseInt(input)System.out.print(Enter
    the second number )input reader.readLine()
    int theSecond Integer.parseInt(input)System.ou
    t.println( The sum of the two numbers is
    (theFirst theSecond))

17
Next Class
  • Conditionals and Decision making.
Write a Comment
User Comments (0)
About PowerShow.com