Docsity

Hello, World! - Computer Science, Lecture notes of Computer science

Toggle sidebar
bg1
The Programming language Java Our First Program Errors Special Characters and Commenting
Hello, World!
Mitsu Ogihara
Department of Computer Science
University of Miami
1/ 36
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Hello, World! - Computer Science and more Lecture notes Computer science in PDF only on Docsity!

Hello, World!

Mitsu Ogihara

Department of Computer Science University of Miami

Table of Contents

(^1) The Programming language Java

(^2) Our First Program

(^3) Errors

(^4) Special Characters and Commenting

The Digital Number Representation

In programming environments, the numbers are often represented using either the binary encoding or the hexadecimal encoding Binary Encoding : the denominations are powers of 2 and at each position the number is either 0 or 1 Hexidecimal Encoding : the denominations are powers of 16 and at each position the number is one of 0 .. 9 and A .. F, where A - F correspond to 10 - 15

Digital Number Examples

Decimal Binary Hexadecimal 0 0 0 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1100 E 15 1111 F 99 1100011 63 100 1100100 64 127 1111111 7F 128 10000000 80

Java Virtual Machine

Java Virtual Machine (JVM) is a program that provides a machine-like environment in which to run Java bytecodes , the kind of program exclusively designed for Java To run Java bytecodes you need Java runtime , a program that runs on JVM

The Software Layers

Hardware (e.g., Macbook Pro)

Operating System (e.g., El Capitan)

Java Virtual Machine

Java Runtime

Compilation & Execution Tools (e.g., shell programs, integrated development environments)

Java Coding Process

(^1) Use a text editor to write the source code (with the file extension of “.java”) (^2) Generate the Java bytecode (with the file extension of “.class”) from the source code (^3) Execute the bytecode

Java Coding Process

(^1) Use a text editor to write the source code (with the file extension of “.java”) (^2) Generate the Java bytecode (with the file extension of “.class”) from the source code (^3) Execute the bytecode

IDEs combine the first two steps into one In a shell environment, the second step is accomplished by executing a line command javac

The First Program: HelloWorld.java

The source code of HelloWorld

1 public class HelloWorld { 2 public static void main( String[] args ) { 3 System.out.println( "Hello, World!" ); 4 } 5 }

The First Program: HelloWorld.java

The source code of HelloWorld

1 public class HelloWorld { 2 public static void main( String[] args ) { 3 System.out.println( "Hello, World!" ); 4 } 5 }

A class is a building block of Java programs This code specifies that “HelloWorld” is a class

Outcome of Execution

In a shell environment you type a command to execute

By the command javac .java, you compile the code for class

By the command java , you execute the bytecode for class

% javac HelloWorld.java % java HelloWorld Hello, World! % Here % is the shell’s output to indicate the start of a new command, called prompt

Structure of a Class

public class { ... }

A method is a named block of code The main in the example is a method has attributes public, static, and void and has a parameter String[] args to run with The class name should be followed by a pair of curly brackets in which the contents appear Each method name followed by a pair of curly brackets in which the contents appear

Structure of a Method

public static void main( String[] args ) { ... }

A statement is an executable unit

A Program with Multiple Statements

1 public class HelloWorld2 { 2 public static void main( String[] args ) { 3 System.out.println( "Hello, World!" ); 4 System.out.println( "Hello, Class!" ); 5 System.out.println(); 6 System.out.println( "This is the fourth line of output." ); 7 } 8 }