Hello, world! Dissect HelloWorld.java Compile it Run it.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Object Oriented Programming in JAVA
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CMT Programming Software Applications
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Program Fundamentals /* HelloWorld.java * The classic “Hello, world!” program */ class HelloWorld { public static void main (String[ ] args) { System.out.println(“Hello,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Computer Systems and the Java Programming Language.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CompSci Today’s topics Parsing Java Programming Notes from Tammy Bailey Reading Great Ideas, Chapter 3 & 4.
Chapter 2 Variables.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
HelloWorld.java import java.awt.*; public class HelloWorld extends
Chapter 2 Variables.
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Computer Programming Methodology Introduction to Java
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
Building Java Programs Chapter 2
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
Expressions and Assignment
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Building Java Programs Chapter 2
Chapter 2 Variables.
General Computer Science for Engineers CISC 106 Lecture 03
Building Java Programs
Presentation transcript:

Hello, world! Dissect HelloWorld.java Compile it Run it

Compiling a program Source code - HelloWorld.java –viewed with an editor –understandable by a human Object code - HelloWorld.class –for Java, this is machine independent byte code –compilers for other languages produce machine code –this is also called the binary form or executable

Compiling Java Compiler HelloWorld.java (source) HelloWorld.class (bytecode) Create HelloWorld.java with an editor Execute the command: javac HelloWorld.java

Running your Java program Once it compiles with no errors, type: java HelloWorld Notice it is not HelloWorld.class. The name here must be the name found after the keyword class in your programs source file. In general it should be the same as the name of the file, minus the extension.

Keywords vs Identifiers Keywords cannot be used for any other purpose. Examples include: class, int, public, static, void Identifiers are the names for things that you get to make up. They must start with a letter and then may include digits. $ and _ (underscore) can be used but should be avoided.

Literals These are strings of symbols that represent “literal” data values. 123 is an integer literal 1.23 is a floating point literal "123" is a String literal as is "class" but class is a keyword and Class is an identifier

Operators and punctuation Operators are symbols like: +, -, / (division), and * (multiply) Punctuation includes symbols like: (, ), {, }, and ; (semicolon)

Data types and variables Data types - simple to complex –int - for integers or whole numbers –double - for numbers with fractional parts –String - for text –Button - a button on a GUI –Point - for representing points in a plane Variables store data in named locations –every variable must have a declared type

Primitive types vs Classes Java has eight primitive types: byte, short, int, long, float, double, char, boolean Primitive types have literal values and can be manipulated with built-in operators. E.g Class type values are created with the operator new. new Button("Quit")

Declaring Variables int count, total; String sentence; boolean done; Button clickToExit;

Initializing Variables int count = 10, total = 0; String sentence = "Hello there."; boolean done = false; Button clickToExit = new Button("Exit");

// HelloWorld2.java - variable declarations class HelloWorld2 { public static void main (String[] args) { String word1; variable String word2, sentence; word1 = "Hello, "; word2 = "world!"; sentence = word1.concat(word2); System.out.println(sentence); }

// StringVsId.java // contrast strings and identifiers class StringVsId { public static void main(String[] args) { String hello = "Hello, world!"; String stringVary; stringVary = hello; System.out.println(stringVary); stringVary = "hello"; System.out.println(stringVary); }

User Input Dissect SimpleInput.java –tio –use + to break up long string literals –be sure an include a prompt –use meaningful variable names –* is multiplication

Calling predefined methods method - a group of instructions with a name. E.g. main(), println(), readInt(). Some methods require some data values upon which to operate. E.g System.out.println(width); These data values are called parameters. Parameters are passed to methods. Some also return a value. E.g. x = Math.sqrt(y);

print() and println() System.out.println("type two integers for" + " the width and height."); System.out.print("type two integers for the"); System.out.println(" width and height."); Illegal System.out.println("type two integers for the width and height.");

Inserting newlines in a string System.out.println("One\nword\nper\line."); Output One word per line.

Numeric Types byte - 8 bits short - 16 bits char - 16 bits (no sign) int - 32 bits - +/-2 billion long - 64 bits - 19 decimal digits float - 32 bits - +/ to +/ digits double - 64 bits - +/ to +/ digits

Numbers vs Strings The bit pattern in the computers memory is different for "1234" and The computer needs to know how to interpret the bits, hence the type for variables. This is just like needing to know what language is being used. Does “pie” mean something good to eat (English), or foot (Spanish)?

Integer arithmetic Dissect MakeChange.java –variable declaration –user input –simple expression –integer division –remainder or modulo

Precedence and associativity * and / have higher precedence than + and - –What is the value of * 3? For equal precedence operators, most are left associative. –What is the value of 100 / 5 * 2? Use parentheses can be used to override the normal rules. E.g. 100 / (5 * 2)