LectureNotes03x

Download Report

Transcript LectureNotes03x

CSE 1341 Honors
Note Set 03
Professor Mark Fontenot
Southern Methodist University
Breakout 1
Overview
 Most Basic Java Program
 Printing to the console
 Variables
 Data types
More About Compiling/Running
MyClass.java
public class MyClass{
//…
}
java MyClass
JVM
javac MyClass.java
MyClass.class
0100101000101
1011010110101
1010100101010
Running Program
Most Basic Java Program
public class BasicJava {
public static void main (String [] args) {
}
}
 Points of interest
 Each set of { } defines a block of code.
 blocks can be nested
 BasicJava is the name of the class
 main method is the entry point
 where the JVM will always begin executing lines of code in your
program
Looking at Java
Class Name: Name of file has to be
name of class with extension .java
Comment – note to others
that read this code about
what it does
public class Hello {
//Say Hello To The World
public static void main (String [] args) {
System.out.println(“Hello World”);
}
}
Output Statement/
Print Statement
Method
System.out.println()
 println() is a function
 It provides the functionality to print whatever is inside the ()
to the terminal (if possible).
 Prints what is in the parentheses, then prints a new line
character so that the next thing that is printed will be on the
next line.
 Examples:
 System.out.println(“Mark Fontenot”);
 System.out.println(“3 + 4 = 7”);
System.out.print()
 print() is a function (as well)…
 like println(), but doesn’t go down to the next line after it
prints
 System.out.print(“hello “);
 System.out.println(“world”);
 System.out.println(”hello world”);
Output
hello world
hello world
Variables
 Variables
 Represent a location in memory (RAM) where we can store
values
 Have a name
 Can be manipulated (changed) in your program
 Must be declared before they can be used
public class VariableTest {
public static void main (String [] args) {
String myName;
int age;
Variable Declarations
float gpa;
}
}
Anatomy of Variable Declaration
String name;
 All variables must have a datatype.
 Indicates what type of data the variable stores and helps the
JVM is setting up storage space for it
 Right now, 9 possible datatypes:
 int, double, long, float, char, boolean, short, String, byte
 Most common for us for now: int, double, String, boolean
 (many more to come… technically an infinite amount possible…
you’ll learn to create your own also!)
Anatomy of Variable Declaration
String name;
 All variables must have a name.
 Variable name is how we access the memory location
 Rules for variable names:
 Must begin with a letter, dollar sign ($), or underscore ( _ ).
 Subsequent characters can be letters, digits, dollar signs, or
underscores
 Keep length reasonable (technically unlimited though)
Anatomy of Variable Declaration(2)
String name;
 Conventions for variable names:
 name should reflect what is being stored
 should be “self documenting”
 Use camel casing… if a variable name has more than one word,
capitalize first letter of each word after first.
 isReady
 myName
Pop Quiz
 Determine if the following are legal
identifiers in Java







&134abx
$_greater
_$hello
gpa1234
2times
%%goAway%%
my Name
Assigning Values to Variables
 Meet the assignment operator:
 =
 Right associative: stores what’s on the right into what’s on the
left.
public class VariableTest {
public static void main (String [] args) {
String name;
int age;
name = “Mark”;
age = 30;
}
}
// ”Mark” = name; wouldn’t make sense
Printing Variables
public class PrintingVariables {
public static void main (String [] args) {
String fName;
String lName;
int age;
fName = “Mark”;
lName = “Fontenot”;
age = 30;
System.out.print(fName);
System.out.print(“ “); //prints blank space
System.out.print(lName);
System.out.println (“ is “ + age + “ .”);
}
}
Printing Variables
public class PrintingVariables {
public static void main (String [] args) {
String fName;
• Uses the ‘+’ operator to put two strings together
String lName;
int age; • What’s the output of this?
fName = “Mark”;
• Can you condense these 4 lines into one print (or
lName = “Fontenot”;
println) stmt ???
age = 30;
System.out.print(fName);
System.out.print(“ “); //prints blank space
System.out.print(lName);
System.out.println (“ is “ + age + “ .”);
}
}
Follow Me in NetBeans
Breakout 2