01.01 L
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

01.01 L

on

  • 805 views

 

Statistics

Views

Total Views
805
Views on SlideShare
805
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

01.01 L Presentation Transcript

  • 1. First Programs, Variables, Integers, and the Assignment Operator Lecture 01.01
  • 2. What Does a Java Program Look Like?
    • public class MyFirstProgram
    • {
    • public static void main(String[] args)
    • {
    • System.out.println(“Hello, world!");
    • }
    • }
    • Note: The program must be saved in a file named MyFirstProgram.java (case sensitive!)
  • 3. print vs. println
    • There are two methods that print to the screen:
      • System.out.print("text here");
      • System.out.println("text here");
    • print leaves the writing marker on the same line.
    • println advances the writing marker to the next line.
  • 4. Examples of print & println
    • The output of this code segment
    • System.out.print("This text is");
    • System.out.print("on the same line.");
    • is
    • This text ison the same line.
  • 5. Examples of print & println
    • The output of this code segment
    • System.out.println("This text is");
    • System.out.print("on another line.");
    • is
    • This text is
    • on another line.
  • 6. What is the output?
    • public class CrazyPrint
    • {
    • public static void main(String[] args)
    • {
    • System.out.println("Can you");
    • System.out.print("guess");
    • System.out.println();
    • System.out.print(" this?");
    • }
    • }
  • 7. Compilation Errors
    • If the syntax is not correct Java syntax, a compilation error will be generated.
    • Example - System.out.prntln("hi"); generates the following error:
      • C:DesktopMyFirstProgram.java:5: cannot find symbol
      • symbol : method prntln(java.lang.String)
      • location: class java.io.PrintStream
      • System.out.prntln("hi");
      • ^
      • 1 error
  • 8. Variables
    • A variable is a named memory location.
    • This code segment prints 56 to the screen:
      • int x;
      • x = 5;
      • System.out.print(x);
      • x = 6;
      • System.out.println(x);
    • Note:
      • int specifies the type of values the variable can hold (in this case, integers)
      • x is the name of the variable
      • A variable must* be declared ( int x ) before it is initialized ( x = 5 ) and initialized before it is used.
  • 9. The Assignment Operator
    • The assignment operator assigns what is on the right side of the operator to the variable on the left.
    • Example code segment that prints 13
      • int x; //declares x
      • x = 5; //assigns 5 to x
      • x = 5 + 5; //assigns 10 to x, x is no longer 5
      • x = x + 3; //assigns 13 to x, x is no longer 10
      • System.out.print(x); //Prints 13 to the screen
  • 10. Legal Identifiers
    • An identifier is used to name variables and other things.
    • A legal identifier is any number of letters (a-z,A-Z), digits (0-9), the dollar sign ($), or the underscore(_). However, the first digit can't be a digit.
    • A Java reserved word can't be an identifier.
  • 11. Example Identifiers
    • x
    • stockPrice
    • firstName
    • I_love_COMP_$ci
    • $_$_$_$
    • Hi12345
  • 12. Incorrect Identifiers
    • 1stName (starts with a digit)
    • First name (two different identifiers)
    • I_Just_Love_* (* is not legal)
  • 13. Identifiers with Style
    • While there are many legal identifiers, in this class the proper style for variables will be to start with a lower case letter and capitalize each word after that.
    • testScore
    • finalAverage
    • player1
    • numberOfTurns
  • 14. Java Reserved Words
    • The following words can not be used as identifiers:
    • abstract continue for new switch
    • assert*** default goto* package synchronized
    • boolean do if private this
    • break double implements protected throw
    • byte else import public throws
    • case enum**** instanceof return transient
    • catch extends int short try
    • char final interface static void
    • class finally long strictfp** volatile
    • const* float native super while
    • *  not used
    • **  added in 1.2
    • *** added in 1.4
    • **** added in 5.0
  • 15. More About Integers
    • The Java virtual machine uses 32 bits (32 0's and 1's) to represent an integer.
    • 1 bit is used as a sign bit (+/-)
    • 31 bits are used for the number
      • Min number is -2 31 = -2,147,483,648
      • Max number is 2 31 -1 = 2,147,483,647
  • 16. Integer Overflow
    • System.out.println(2147483647);
    • prints the number correctly
    • System.out.println(2147483648);
    • causes a compilation error
    • System.out.println(2147483647+1);
    • prints -2147483648
    • System.out.println(2147483647+2);
    • prints -2147483647
    • System.out.println(214748364*100);
    • prints -80
  • 17. Adding Comments to Code
    • Comments are notes that are added to a program that the compiler will ignore
    • There are two styles
      • // comments out from // to end of the line
      • /* */ comments out everything from /* to */
    • Examples:
      • int totalSales; //total sales for the fiscal quarter
      • /* print error msg */ System.out.println("***");