public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); }}
Difference between System.out.println() and System.out.print()
javac Hello.java
java Hello
Java – method signature
The main method looks like this:
public static void main(String[] args) { }
public is an access modifier (private is another option)
static – the method belongs to the class itself rather than to an instance of that class
void is what the method returns (nothing)
String[] args is the parameter, an array of String
Java Program Structure
Everything is in a class – what is a class? (Well, there are packages too, but later)
No class, no program.
Every program must have a main() method.
Why? main() is the execution entry point
Programs typically consist of many classes
Each class could have a main() method
So, then, which main() runs?
Typing
Python does dynamic typing
word ="car"number =3new_list = []names = ["Pedro", "Melissa", "Jessica"]
Static typing in Java
String word = "car";int number = 3;int[] numbers = new int[10];String[] names = new String[]{"Pedro", "Melissa", "Jessica"};
Try it out
Modify your main method in your HelloWorld class to assign the string literal "Hello World!" to a String variable name, and then print out that variable instead.
Questions:
Can you use both double and single strings to create the variable?
Does concatenation with the + operator work?
Can we concatenate (glue together) String, char and int (or a combination of these)?
Java variables
Declare any time before use, and strongly-typed
public class Hello { public static void main(String[] args) { String word = "Hello World!"; System.out.println(word); }}
Java variables
Declare any time before use
Strongly-typed
If you don’t assign a value it’s probably a bug
in some context a type-specific default is used
Two “flavors” of variable: primitive (often called scaler or base) and object (reference)
Java primitive types
boolean
char (unicode) – single quotes
int
short and byte for smaller ints
long for longer ints
double
float for shorter floats
Primitive type names are lower-case
Java primitive types
These act like c++ variables
variable literally and directly stores value
Java Data Types and Memory Space
1 byte is 8 binary digits
short (2 bytes)
long (8 bytes)
double (8 bytes)
boolean (1 bit)
int (4 bytes)
float (4 bytes)
byte (1 byte)
char (2 bytes)
All Objects (everything else) (4 bytes)
Mathematical Operations
All operators as you would expect:
mathematical: +*-/%
No // in Java. Division type will be based on input types
int divided by an int is an int
Only works for number types
comparison: <><=>=
numbers only
Mathematical Operations
equality: ==!=
behave as expected for primitives
behave differently for objects
logical: &&||!
back to the symbols (instead of words in Python)
Style and documentation
variable names often camelCase
white space (new lines, indendation) means nothing, but should be used for style (to make code easier to read)
Java requires a lot of code to express a simple concept.
You need to be much more explicit when writing Java than Python (more words to type in Java)
Style and documentation
// one line comment/* multi-line comment *//** * JavaDoc comments for external documentation * @returns description of what returns */