Java Basics Part I - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Java Basics Part I

Description:

More on these when we talk about classes. Slide 19. Lastra, 2001. Assignment Statement ... Read in a series of positive and negative integers from the console. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 33
Provided by: anselmo9
Category:
Tags: about | basics | java | part | the

less

Transcript and Presenter's Notes

Title: Java Basics Part I


1
Java BasicsPart I
  • Thursday, January 9

2
Red Tape
3
Readings
  • Stop at Weiss Section 2.4
  • Some might find 2.5 confusing
  • Cover later
  • Read
  • Weiss Ch. 1
  • Weiss Sections 2.1 2.4 by Th 1/17

4
Java Applications vs. Applets
  • Very similar except Applet runs in browser
  • Well write applications.
  • Initially console apps

5
A Simple Program
  • /
  • A very simple Java program.
  • /
  • public class Class1
  • public static void main (String args)
  • System.out.println("Hello Java")

6
Classes
public class Class1
  • Everything in Java is part of a class.
  • A class is a blueprint for objects.
  • A program contains at least one class.

7
The Main Method
public static void main (String args)
  • The main( ) method (in a class) is where the java
    program begins.
  • You must define one.

Except in an Applet
8
Comments
  • Any text after // is ignored
  • Text between / and / is ignored.

// This is a comment public static void main()
/ This is too /
9
Identifier Names
  • Must begin with letter, underscore ( _ ) or
    dollar ( ).
  • Example 2a is not legal
  • Cant contain operators (, -, etc.)
  • Java uses Unicode, so a letter has a broader
    definition. For example, ? is legal.
  • Questionable usage, though.

10
Case
  • Case matters!
  • VariableName ? variablename

11
Reserved Words
  • You cant use reserved words as identifiers.
  • Examples if, else, while, static class.
  • Note that
  • Class ? class

12
Declaring Variables
  • Pretty standard
  • int radius
  • char letter
  • Modifiers later

13
Variables
  • Unlike C, no global variables
  • Must all be in a class.

14
Initializing Variables
  • Again, very standard
  • int i 0
  • Variable initialization makes program easier to
    read and understand

15
Numeric Data Types
  • byte
  • short
  • int
  • long
  • float
  • double

8 bits 16 bits 32 bits 64 bits 32 bits 64 bits
-128 to 127 -32,768 to 32,767 -2 billion to 2
billion -263 to 263 - 1 6 digits, exp 10-46 to
1038 15 digits, exp 10-324 to 10308
16
Constants
  • static final double PI 3.14159
  • Why constants?

17
Naming guidelines
  • Class
  • ClientData
  • Variables and methods
  • radius
  • readDouble( ),
  • Constants
  • PI

18
Modifiers
  • public visible to any class
  • private only visible in this class
  • static defines a class method or data field.
  • final cant be modified
  • More on these when we talk about classes

19
Assignment Statement
  • Assignment operator is , not (or lt-)
  • i 7
  • Can get arbitrarily confusing
  • i j k l p
  • Variable must be on the left
  • 1 i not correct.

20
Expressions
  • Conventional
  • x 3 y
  • Operators like C and C
  • - /
  • No operations on arrays

21
Shortcut operators
  • Like C
  • --
  • Example
  • x 3
  • y x
  • What are values of x and y?
  • May also be prefix, like x

22
Prefix and Postfix
  • Example
  • x 3
  • y x
  • z x
  • What are values of x, y and z?

23
Precedence
  • Prefix and -
  • Operators , /, and
  • Operators and -
  • Postfix and --
  • Use ( ) to group when result is obscure!

24
Type conversion
  • Java automatically converts less accurate types
    to more accurate ones in an expression
  • byte i 100
  • long l i 3 4
  • double f i 3.1 1 / 2
  • Will not automatically convert the other way

25
Explicit Type Conversion
  • double f 10.3
  • int i (int ) f
  • Note that number got truncated.
  • Results can be surprising
  • int i 257
  • byte s (byte )i
  • Whats the result?

26
Character Data Type
  • Used to represent a single character
  • char letter A
  • char number 5
  • Characters enclosed in single quotation marks.
  • They are Unicode.
  • Some special characters
  • Newline \n Carriage return \r
  • Backspace \b Tab \t

27
Program 0
  • Not to be handed in (unless you want help).
  • Target date Thursday, January 17
  • Task
  • Read in a series of positive and negative
    integers from the console.
  • Entry stops when user types a zero.
  • Print sum, as well as number of positive and
    negative integers entered.

28
What Youll Need
  • Basic console I/O
  • Output
  • system.out.println( )
  • Input next slide.

29
Input
  • Ive provided a simple Class called Keyboard
  • Same interface as in COMP14.
  • int Keyboard.readInt()
  • int Keyboard.readDouble()
  • Demonstration project Keyboard Example
  • Reads in only single numbers on a line.
  • Bonus
  • void Keyboard.pause()

30
Resources
  • http//java.sun.com
  • Documentation
  • Code
  • Online Java tutorial
  • http//java.sun.com/docs/books/tutorial/?frontpa
    ge-spotlight

31
Recitation Tomorrow
  • How to use Visual J
  • demonstration
  • Introduction to Java
  • follow-up to what we talked about today

32
Next Tuesday
  • Binary numbers
  • More Java
Write a Comment
User Comments (0)
About PowerShow.com