Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

Similar presentations


Presentation on theme: "Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)"— Presentation transcript:

1 Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

2 Our First Java Program Java programs are written in a text editor or IDE and saved with a “.java ” filename extension // This is Java public class Simple { public static void main( String[] args ) { System.out.println( "Hello!" ); } Save this file as Simple.java (names must match) This is a comment Curly brackets (a.k.a. braces) must match up This line of code prints a message to the console window

3 Comments Add comments to Java programs to describe what your program does This helps others (and you!) understand your code Information to include: Your name(s) Date Description Version etc.

4 Comments Types of comments in Java include: Line-comments Block comments “Javadoc” comments // This is a line-comment /* This is a block-comment */ /** * Describes a section of Java code and * automatically generates HTML documentation */

5 Compiling a Java Program public class Simple {... Java program Java Compiler 00010100 00110010 10000100 01001001 01010101 01010010 Java byte code (“.class” file)

6 Executing a Java Program 00010100 00110010 10000100 01001001 01010101 01010010 Java byte code (“.class” file) 00010100 00110010 10000100 01001001 01010101 01010010 Precompiled libraries (i.e. byte code) Java Virtual Machine

7 Keywords in Java Java contains keywords that are understood by the Java compiler e.g. public, static, class, void, etc. Keywords are always lowercase Keywords cannot be used for any other purpose (e.g. for class names, variable names, etc.)

8 Keywords in Java abstract assert boolean break byte case catch char class const continue default do double else enum extends false for final finally float goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

9 What’s in a Name? Names are important in a Java program A class name uniquely identifies a Java program Class names must start with a letter and contain only letters and numbers Names could also contain underscore ‘ _ ’ or dollar sign ‘ $ ’ characters, but these should be avoided public class WelcomeToJava {... }

10 Class and Method Headers Java programs usually contain a class header and a method header The class header identifies the name of the class (i.e. program) and where it begins: The method header identifies the name of the method (e.g. main ) and where the method begins: public class Simple { public static void main( String[] args ) {

11 Java Statements A Java statement is a line of valid Java code Most Java statements must end in a semicolon ‘ ; ’ Class and method headers must not end with a semicolon System.out.println( "Hello!" ); System.out.print( "Welcome to" ); System.out.println( " Java." );

12 Java Output Use print and println to display output to the console window Use println to go to the next line Sample output: System.out.println( "Hello!" ); System.out.print( "Welcome to" ); System.out.println( " Java." ); Hello! Welcome to Java.

13 Java Output Also use special Java escape sequences to go to the next line, tab forward, etc. \n newline Advances the cursor to the next line for subsequent printing \t tabCauses the cursor to skip over to the next tab stop \\ backslashCauses a backslash to be printed \' single quoteCauses a single quotation mark to be printed \" double quoteCauses a double quotation mark to be printed

14 Java Output Incorporate Java escape sequences into the text you display Sample output: System.out.println( "\tHello!\n" ); System.out.print( "Welcome to" ); System.out.println( " \"Java\"" ); Hello! Welcome to "Java"

15 Java Data Types A data type represents a set of allowed values (and operations on those values) Data type int is a numerical data type representing integers Data type float is a numerical data type representing floating-point numbers Data type double is a numerical data type representing double-precision floating-point numbers Other data types are byte, short, long, boolean, and char

16 Java Data Types byte 1 byteIntegers in the range -128 to +127 short 2 bytesIntegers in the range of -32,768 to +32,767 int 4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647 long 8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 float 4 bytesFloating-point numbers in the range of ±3.410E-38 to ±3.410E38, with 7 digits of accuracy double 8 bytesFloating-point numbers in the range of ±1.710E-308 to ±1.710E308, with 15 digits of accuracy use int for your integers use double for your floating-point numbers

17 Java Data Types A data type represents a set of allowed values and operations on those values Arithmetic operators include addition ( + ), subtraction ( - ), multiplication ( * ), division ( / ), and modulus division ( % ) Binary operators require two operands (e.g. 9 * 87 ) Unary operators require one operand (e.g. -37 ) modulus division calculates the remainder after integer division (e.g. 9 % 2 equals 1 ) negation operator

18 Variables As in mathematics, a variable is a symbolic name used to represent numeric (or other) values In Java, variables must be declared and must have a specific data type: int x; float y; double average; long q; specify the data type specify the variable name

19 Assigning Values to Variables Assign values to variables using the = operator: int x; float y; double average; long q; x = 47; y = -12.375F; average = 42.7; q = 875L; specify the variable name specify the value

20 Displaying Variables as Output Use print and println to display the values of variables: Sample output: System.out.print( "Value of x is: " ); System.out.println( x ); System.out.print( "Value of y is: " ); System.out.println( y ); Value of x is: 47 Value of y is: -12.375

21 Displaying Variables as Output Use the concatenation operator ( + ) to simplify your print and println statements: Sample output: System.out.println( "Value of x is: " + x ); System.out.println( "Value of y is: " + y ); Value of x is: 47 Value of y is: -12.375


Download ppt "Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)"

Similar presentations


Ads by Google