Java Programming Basics - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Java Programming Basics

Description:

If we are making a Java file that contains a public class Lab1, what should file ... In Java, though, we can have variables of ... Java's Primitive Data Types ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 14
Provided by: jonc7
Category:

less

Transcript and Presenter's Notes

Title: Java Programming Basics


1
Java Programming Basics
First, a QUIZ. Then, new stuff.
2
Questions from the last few classes
  • Who makes the Java programming language?
  • If we are making a Java file that contains a
    public class Lab1, what should file be saved as?
  • What is the main method used for?
  • What is the import statement used for?
  • What did we use to print out a line to the
    command line?
  • What are the three steps to creating and running
    a Java program?
  • What is an IDE?
  • The JVM?

3
Questions you should get the answer to from this
class
  • What is a variable?
  • What are the primitive data types in Java and how
    much memory does it take to hold each type?
  • What math operators can we use in Java?

4
Variable
  • Just like in Algebra, a variable is something
    that represents something else
  • In Algebra,x and y always had to represent a
    number. You never had to worry about them being
    characters (letters) or anything else.
  • In Java, though, we can have variables of all
    different types. Therefore, before we use a
    variable, we have to declare it with both a type
    and an identifier.

5
a type and an identifier?
  • When we say declare it with a type and an
    identifier, that means we have to say what kind
    of data the variable will be representing, and
    what we are going to call the variable.
  • For instance int x
  • char y
  • String args
  • double num

Type
Identifier
6
Identifiers
  • An identifier (the name of the variable) must
    start with a letter, an underscore ( _ ), or a
    dollar sign ()typically, though, every variable
    you see starts with a letter
  • You cant start an identifier with a number, or
    contain an operator ( , -, , etc)
  • An identifier cant be a reserved word (those
    blue words in JCreator)
  • As a good coding practice, the identifier should
    be something that describes what the variable
    will be used for. For example, if a variable will
    be used to hold an average of several numbers,
    name it something like average, avg, or mean.

7
Types
  • There are two kinds of data types in Java
    primitive data types and objects.
  • When a variable is going to represent a primitive
    data type, the type is always in lowercase
  • int num
  • When a variable represents an object, the type is
    generally capitalized
  • String word
  • Rectangle rec

8
Javas Primitive Data Types
  • Four of the primitive types represent whole
    numbers of different sizes
  • byte -128 to 127 (remember, a byte is 8 bits,
    like 11111111. In order to work with negative
    numbers, the last bit is the sign bit positive
    or negative, so that leaves seven bits to hold
    the value. 011111112 12710 )
  • short -32,768 to 32,767
  • int -2,147,483,648 to 2,147,483,647
  • long -9,223,372,036,854,775,808 to
    9,223,372,036,854,775,807
  • Two of the data types represent real numbers
    (numbers with decimals)
  • float -3.4E38 to 3.4E38 (6 to 7 digits of
    accuracy)
  • double -1.7E308 to 1.7E308 (14 to 15 significant
    digits)
  • One character type char, for characters in single
    quotes, like 'a'
  • And one boolean (true or false) type

9
Javas Primitive Data Types(that you need to
know)
  • boolean (1 bit of storage) true or false, 0 or 1
  • char (2 bytes of storage) 0 to 65,535, which
    correspond to the Unicode character codes. Digits
    0 to 125 of Unicode correspond to ASCII, which
    can be found at www.asciitable.com.
  • int (4 bytes of storage) -2,147,483,648 to
    2,147,483,647
  • double (8 bytes of storage) -1.7E308 to 1.7E308

10
Using Primitive Data Types
  • public static void main(String args)
  • //declares an int (whole number) named val and
    gives it the value 5
  • int val 5
  • //declares a char (character) named letter and
    gives it the value 'c'
  • char letter 'c'
  • //declares a decmal number (double) named num w/
    value 0.365252
  • double num 0.365252

11
Math Operators in Java
  • You've seen the used to assign values to
    variables
  • There are other important operators in Java you
    can use on numerical variables (ints and
    doubles)
  • //addition
  • - //subtraction
  • //multiplication
  • / //division
  • () //to specify order of operations

12
Using Math Operators in Java
  • Order of operations in Java is the same as it is
    in Algebra.
  • What does each line of code result in?
  • int x 4
  • int y 2
  • int answer x y
  • answer x x y
  • answer (x x) y
  • answer ((x x) y)/4
  • System.out.println("answer "answer)

13
imprecision of double (floating point)
computation
  • Since doubles have 8 bytes of storage to store a
    HUGE set of decimal numbers (-1.7E308 to
    1.7E308), it isn't possible to store each and
    every possible number. So doubles in Java can be
    thought of as APPROXIMATIONS of numbers
  • when you do calculations on doubles, you can't
    expect EXACT solutions
  • double num 0.365252
  • System.out.println(num) //prints out "0.365252"
  • num num 0.01
  • System.out.println(num) //prints out
    "0.37525200000000003"
Write a Comment
User Comments (0)
About PowerShow.com