1 / 18

CSE 1341 Honors

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.

carver
Download Presentation

CSE 1341 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 Honors Note Set 03 Professor Mark Fontenot Southern Methodist University

  2. Breakout 1

  3. Overview • Most Basic Java Program • Printing to the console • Variables • Data types

  4. More About Compiling/Running MyClass.java public class MyClass{ //… } java MyClass JVM javacMyClass.java MyClass.class Running Program 0100101000101 1011010110101 1010100101010

  5. 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) { } }

  6. 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

  7. 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”);

  8. 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

  9. 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

  10. 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;

  11. 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;

  12. 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;

  13. Pop Quiz • Determine if the following are legal identifiers in Java • &134abx • $_greater • _$hello • gpa1234 • 2times • %%goAway%% • my Name

  14. 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; } }

  15. 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 + “ .”); } }

  16. 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 ???

  17. Follow Me in NetBeans

  18. Breakout 2

More Related