180 likes | 299 Vues
This document serves as an overview of fundamental Java programming concepts, focusing on the basics of compiling and running a simple Java program using the `MyClass.java` example. It explains key elements such as the structure of a Java class, the `main` method as the entry point, and variable declarations. The guide illustrates how to print to the console using `System.out.println()` and `System.out.print()`, and covers the anatomy of variable declarations and legal identifier rules in Java. This is an essential resource for beginners aiming to understand Java syntax and structure.
E N D
CSE 1341 Honors Note Set 03 Professor Mark Fontenot Southern Methodist University
Overview • Most Basic Java Program • Printing to the console • Variables • Data types
More About Compiling/Running MyClass.java public class MyClass{ //… } java MyClass JVM javacMyClass.java MyClass.class Running Program 0100101000101 1011010110101 1010100101010
Most Basic Java Program • Points of interest • Each set of { } defines a block of code. • blockscan 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 public class BasicJava { public static void main (String [] args) { } }
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”); } } Method Output Statement/ Print Statement
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 linecharacter 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; float gpa; } } Variable Declarations
Anatomy of Variable Declaration • 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!) String name;
Anatomy of Variable Declaration • 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) String name;
Anatomy of Variable Declaration(2) • 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 String name;
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”; // ”Mark” = name; wouldn’t make sense age = 30; } }
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; 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 + “ .”); } } • Uses the ‘+’ operator to put two strings together • What’s the output of this? • Can you condense these 4 lines into one print (or println) stmt ???