Lecture 03 Java Intro

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Java Intro

  • Let’s start by comparing the syntax of the prerequisite topics in Python vs Java

Comments

  • Comments are lines of code which are only used to communicate to programmers
  • Comments do not effect the output of a program
  • Can add more context to a program

Python

# This is a comment
x = 5 # This is an inline comment

Java

// This is a comment
int x = 5; // This is an inline comment

Differences

Python

# This is a comment
x = 5 # This is an inline comment

Java

// This is a comment
int x = 5; // This is an inline comment
  • Java: all variables declarations must have an explicit type
  • Java: most statements must end with a semicolon(;)

Variables

  • Variables store information
  • This can be numbers, string, lists and more
  • Variables can have their values changed as the program runs

Python

x = 5     # x is 5
y = 6 + 3 # y is 9
z = y * 2 # z is 18

Java

int x = 5;     // x is 5
int y = 6 + 3; // y is 9
int z = y * 2; // z is 18

If Statements

  • Allows a program to take branching paths

Python

x = 5
if x > 5: 
    # If x is greater than 5
    # Then do these statements
    print("Greater than 5")
elif x == 5: 
    # Else if x is equal to 5
    # Then do these statements
    print("Equal to 5")
else:
    # Else do these statements
    print("Less than 5")

Java

int x = 5;
if (x > 5) { 
    // If x is greater than 5
    // Then do these statements
    IO.println("Greater than 5");
} else if (x == 5) { 
    // Else if x is equal to 5
    // Then do these statements
    IO.println("Equal to 5");
} else { 
    // Else do these statements
    IO.println("Less than 5");
}

Differences

Python

x = 5
if x > 5: 
    # If x is greater than 5
    # Then do these statements
    print("Greater than 5")
elif x == 5: 
    # Else if x is equal to 5
    # Then do these statements
    print("Equal to 5")
else:
    # Else do these statements
    print("Less than 5")

Java

int x = 5;
if (x > 5) { 
    // If x is greater than 5
    // Then do these statements
    IO.println("Greater than 5");
} else if (x == 5) { 
    // Else if x is equal to 5
    // Then do these statements
    IO.println("Equal to 5");
} else { 
    // Else do these statements
    IO.println("Less than 5");
}
  • Java: if conditions must be inside parenthesis
  • Java: no elif instead use else if
  • Java: Code inside if block is denoted using curly braces({})

Looping

  • Allows a program to repeat a sequence of statements many times
  • While loops repeat the body of the loop until the condition is false
  • Loops can repeat as many times as they want

Python

i = 0
while i < 5: 
    # Loop until i is NOT less than 5
    # While the condition holds do 
    # these statements
    print(i)
    i = i + 1
    # Then check the condition again 
    # and repeat if necessary

Java

int i = 0;
while (i < 5) {
    // Loop until i is NOT less than 5
    // While the condition holds do 
    // these statements
    IO.println(i);
    i = i + 1;
    // Then check the condition again 
    // and repeat if necessary
}

Indentation

  • In Python indentation matters
  • In Java indentation doesn’t matter
  • Instead curly braces ({}) are used to denote which statements are inside a block of code

Python

x = 5
y = 6
if x < 5:
    x = 7 # Inside

y = 8 # Outside
print(x) # Prints 5
print(y) # Prints 8

Java

int x = 5;
int y = 6;
if (x < 5) {
    x = 7; // Inside
}
y = 8; // Outside
IO.println(x); // Prints 5
IO.println(y); // Prints 8

Indentation

Python

x = 0
y = 0
while x < 10:
    x += 1   # Inside
    print(x) # Inside
print(y) # Outside

Java

int x = 0;
int y = 0;
while (x < 10) {
    x += 1;        // Inside
    IO.println(x); // Inside
}
IO.println(y); // Outside

Input

  • Getting user input in Python and Java is easy

Python

# Read an int from user input
x = int(input())
# Now you can use the number
if x >= 0:
    print("Positive")

else:
    print("Negative")

Java

// Read an int from user input
int x = Integer.parseInt(IO.readln());
// Now you can use the number
if (x >= 0) {
    IO.println("Positive");
}
else {
    IO.println("Negative");
}