Simple Java Program. public class HelloWorld {

Similar documents
Full file at

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CS 11 java track: lecture 1

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Basics of Java Programming

Program Fundamentals

Java Programming. Atul Prakash

COMP 110 Introduction to Programming. What did we discuss?

Accelerating Information Technology Innovation

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

4 Programming Fundamentals. Introduction to Programming 1 1

Getting started with Java

Object-Oriented Programming

These are reserved words of the C language. For example int, float, if, else, for, while etc.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Programming with Java

An overview of Java, Data types and variables

COMP-202: Foundations of Programming

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

MODULE 02: BASIC COMPUTATION IN JAVA

Java Bytecode (binary file)

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Lecture Set 4: More About Methods and More About Operators

Computer System and programming in C

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Java Notes. 10th ICSE. Saravanan Ganesh

Zheng-Liang Lu Java Programming 45 / 79

Lecture Set 4: More About Methods and More About Operators

3. Java - Language Constructs I

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS313D: ADVANCED PROGRAMMING LANGUAGE

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Computer Components. Software{ User Programs. Operating System. Hardware

Variables. Store information needed by the program

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

DATA TYPES AND EXPRESSIONS

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Declaration and Memory

Getting Started Values, Expressions, and Statements CS GMU

QUIZ: What value is stored in a after this

Introduction to Programming (Java) 2/12

Fall 2017 CISC124 9/16/2017

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Chapter 2 Elementary Programming

Java Identifiers, Data Types & Variables

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Visual C# Instructor s Manual Table of Contents

CMPT 125: Lecture 3 Data and Expressions

JAVA Programming Fundamentals

Chapter 3: Operators, Expressions and Type Conversion

Language Fundamentals Summary

CSC 1107: Structured Programming

Full file at

Variables and literals

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java Basic Programming Constructs

Programming in C++ 4. The lexical basis of C++

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

1 Lexical Considerations

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

LECTURE 3 C++ Basics Part 2

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Important Java terminology

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Types, Operators and Expressions

More Programming Constructs -- Introduction

UNIT- 3 Introduction to C++

Basic Computation. Chapter 2

The MaSH Programming Language At the Statements Level

Decaf Language Reference Manual

CMSC131. Introduction to your Introduction to Java. Why Java?

Lexical Considerations

The C++ Language. Arizona State University 1

PROGRAMMING FUNDAMENTALS

Programming. Syntax and Semantics

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lexical Considerations

Datatypes, Variables, and Operations

Chapter 2: Data and Expressions

Chapter 2. Lexical Elements & Operators

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Should you know scanf and printf?

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Transcription:

Simple Java Program file: HelloWorld.java public class HelloWorld { } public static void main (String[] args) { //our instructions go here System.out.println("Hello, World!"); } Each word of this should make sense by the semester's end! For now it is boilerplate code just the template we'll use to write code.

Whitespace Whitespace means the 'blank' characters: space, tab, newline characters. White Space is (almost) irrelevant in Java. spaces used to separate idencfiers (int x vs intx) we can't span lines within Strings. (no typing <enter> between " ") Syntax is not based on indentacons but indentacon is highly recommended! (required for class)

Bad Whitespace Example #1 public Spacey { public stacc Valid, but horribly wriven, code. (excessive, meaningless spacing) class void main(string[ ] args ){ System. out.println("weirdly-spaced code scll runs!" );}}

Bad Whitespace Example #2 Valid, but horribly wriven, code. (one-liners aren't always best!) public class Spacey2{public static void main(string[]args){system.out. println("space-devoid code also runs...");}} (the above is all on one line of text in Spacey2.java) Code like this might not receive any credit! Seriously, don't do this in anything you ever turn in. Never make the grader unhappy.

Good Whitespace Example public class GoodSpacing {" public static void main (String[] args) {" int x = 5;" int y = 12;" System.out.println("x+y = "+ (x+y));" }" } indentagon levels for each block: class, method definigons, control structures

Identifiers Identifiers are the 'names' we choose for variables, methods, classes, interfaces, etc. An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign ($) Identifiers cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers 1-7

Identifiers Identifiers cannot be keywords By convention, programmers use different case styles for different types of identifiers, such as lower case for variables response, previousindex title case for class names Person, MasonStudent upper case for constants MASON, MAX_INT

IdenCfier Examples Legal IdenCfier Examples: Illegal IdenCfier Examples: hello camelcasename $_09abizzare user_input18 anyarbitrarilylongname two words Extra-Characters! 1st_char_a_number transient (it's a keyword) Dots.And.Hooks?

Java Keywords Keywords are idencfiers that have meanings in the language definicon. They cannot be used by programmers as new idencfiers.

Types! Java is strongly typed: every expression has a specific type that is known at compile Cme, and never changes. (whether it's a variable, literal value, method call, or any other expression) Java has two kinds of types: primi3ve types (containing literal values) reference types (containing objects of some class).

PrimiCve Types PrimiCve Types contain the basic values of the language, such as numbers, characters, and booleans. boolean: truth values. Only possible values: true, false. char: one character in single-quotes: 'a' 'H' '\n' '5' numbers: there are many versions of integers (all with limited ranges), and two versions of real numbers (different precision/ranges).

Specific Integer Types Java provides integral numbers of different 'widths' (different numbers of bits), and different ranges of values: byte (8 bits) -128 to 127 short (16 bits) -32768 to 32767 int (32 bits) -2147483648 to 2147483647 long (64 bits) (-2 63 ) to (2 63-1) char (16 bits) 0 to 65535 (all posicve) (can edit char as its Unicode #, but it sgll is printed as its current character, not the code number) The compiler doesn't allow too-big numbers in each type. To specify long values, add an 'L' to the end: (or 'l', which looks like a 1) 123412341234L 037L 0x345L 100000L

Literal Integer RepresentaCon Integers can be represented in decimal, hexadecimal, or octal. (different representagons of the same values!) decimal: start with 1-9, followed by 0-9. (or just the 0 value.) 0 10 483 9876501234 66045 hexadecimal (base 16): prefix 0x, followed by one or more of 0123456789ABCDEF. (also, a-f are equivalent to A-F).

Note on Different RepresentaCons All three inputs are alternacves you can use to describe the same values. You also know: Roman Numerals tally-marks (e.g., XLVI) (base 1 councng) à All of these represent integers! Don't confuse representacon with meaning.

FloaCng Point Numbers Approxima3ng the real #s: called floacng point numbers. We just write things in normal base 10 as always. internal sign binary exponent representacon: fraccon like sciencfic notacon. S: sign bit. E: bias-adjusted exponent. M: adjusted fracconal value. value = (-1) S * 2 E * M Also representable: infinity (+/ ), NaN ("not a

RepresenCng FloaCng Point Numbers FloaCng Point numbers: - may have a decimal point followed by digits 2.32 1.21 450000 - may be wriven in sciencfic notacon: 2.32e5 = 2.32x10 5 = 232000 - may be very large: 2E35F 2e250-2e250 float: use f suffix (or F) to indicate float. 0f 3.14159f -2E3F 59023f double: floacng point numbers are doubles by default. 0.3 3.141592653589-3.15E30

PracCce Problem To what types can these numbers be stored? char byte short int long float double 0 150 3.14 7e200 0x3F 371F float char short int long float double double double byte short int long float double double

CreaCng Variables Variables must be declared and ini3alized before use. Declara3on: creates the variable. It includes a type and a name. The variable can only hold values of that type. int x; char c; boolean ok; Person p; Ini3aliza3on: assign an expr. of the variable's type to it. x=7+8; c='m'; ok = true; p = new Person(); Both: we can declare and instancate all at once: int x = 5; char c = 'S'; Person p = new Person();

CasCng With all these numerical types, changing between them is possible, and has implicacons. a cast is a conversion from one type to another. We cast things by placing the type in parentheses in front of it: (int) 3.14f One use: forcing floacng-point division. int x=3, y=4; double z = ((double)x)/y; System.out.println(z); //prints 0.75

Java Comments There are two main styles of comments in Java: Single-Line: from // to the end of the line. Mul3-Line: all content between /* and */, whether it spans mulcple lines or part of one line. JavaDoc: convengon of commengng style that helps generate code documentagon/api. More on this later.

Expressions, Statements Expression: a representacon of a calculacon that can be evaluated to result in a single value. There is no indicacon what to do with the value. Statement: a command, or instruccon, for the computer to perform some accon. Statements oen contain expressions.

Basic Expressions literals (all our numbers, booleans, characters) operacon exprs: < <= > >= ==!= (relaconal ops) + - * / % (math ops) &&! (boolean ops) e? e : e (ternary condiconal expr) variables parenthesized expressions ( expr )

CondiConal Expression The?: ternary operator is a condiconal expression: it evaluates the boolean expression, and then results in the middle expression when true, and the last expression when false. boolexpr? expr : expr We must have all three parts, and the 2 nd and 3 rd expressions' types must agree with (fit in) the resulcng type of the encre expression.

Expression Examples Legal: 4+5 (3>x) && (! true) x%2==1 (x<y)&&(y<z) numpeople drawcard() (2+3)*4 y!=z Illegal (these aren't expressions): x>y>z 4 && false x=3 7(x+y)

Basic Statements DeclaraCon: announce that a variable exists. Assignment: store an expression's result into a variable. method invocacons (may be stand-alone) blocks: mulcple statements in { }'s control-flow: if-else, for, while, (next lecture)

Statement Examples int x; // declare x x = 15; // assignment to x int y = 7; // decl./assign. of y x = y+((3*x) 5); // assign. with operators x++; // increment stmt (x = x+1) System.out.println(x); // method invocacon if (x>50) { x = x 50; } else { y = y+1; } //if-else statement