1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 1: Computer Systems
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
The Java Programming Language
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Outline Java program structure Basic program elements
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Hello, world! Dissect HelloWorld.java Compile it Run it.
String Escape Sequences
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;
Chapter 1 Introduction.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
© 2006 Pearson Education Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Java Language and SW Dev’t
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
The Java Programming Language
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Introduction to Java Java Translation Program Structure
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
By Mr. Muhammad Pervez Akhtar
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
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.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Working with Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
Data types and variables
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Focus of the Course Object-Oriented Software Development
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Instructor: Alexander Stoytchev
Presentation transcript:

1 Java Basics

2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from C or Assembly to machine code Java must be (explicitly) compiled before it is run The Java compiler turns Java source code (.java) into Java bytecode (.class)

3 The Java Platform The Java Virtual Machine (JVM) is responsible for running bytecode The idea: bytecode can be interpreted quickly The same bytecode can be interpreted on any architecture: write once, run anywhere Code (C,C++) compiled to machine code must be compiled to a specific system

4 The Java Language Created by Sun Microsystems Introduced in 1995, initial popularity grew due to Internet applications  Excitement surrounding Java applets  Confusion with Javascript Steady rise in popularity has continued for “better” programming reasons

5 A Historical Interlude: The Java Team Java originally intended to be used on “smart” consumer electronics Bill Joy  Founded Sun, 1982  Intelligent robots will replace humanity in the near future… James Gosling (“the father of Java”)  University of Calgary grad  First JVM, compiler, interpreter  also developed Emacs Patrick Naughton  Arrested in late 90s on child predator charges  Not mentioned so much as a founding father anymore

6 The Java Language (cont’d) … is a high-level programming language … is very object oriented … is similar to C++ and C … typically compiled to Java bytecode … is often confused with the Java Platform, but these are two different aspects of “Java”

7 Syntax and Semantics The syntax rules of a language define how we can combine reserved words, symbols, and identifiers The semantics of a program statement define what the statement means  Problem with program syntax = “error”  Problem with program semantics = “bug”

8 Java Program Structure A Java program consists of:  One or more classes  A class contains one or more methods  A method contains program statements We will explore these terms in detail

9 Java Program Structure public class MyProgram {}{} // comments about the class class header class body Comments can be placed almost anywhere

10 Java Program Structure public class MyProgram {}{} // comments about the class public static void main (String[] args) {}{} // comments about the method method header method body

11 Hello World public class HelloWorld {}{} // HelloWorld.java public static void main (String[] args) {}{} System.out.println(“Hello World!”);

12 Hello World public class HelloWorld {}{} // HelloWorld.java public static void main (String[] args) {}{} System.out.println(“Hello World!”); Creates a “class” called HelloWorld  Compiled to HelloWorld.class  Classes used to define objects… later

13 Hello World public class HelloWorld {}{} // HelloWorld.java public static void main (String[] args) {}{} System.out.println(“Hello World!”); The “main” method is where it starts to run  Ignore “public static void” and “String[] args” for now

14 Hello World public class HelloWorld {}{} // HelloWorld.java public static void main (String[] args) {}{} System.out.println(“Hello World!”); Contains one “statement”  The System.out.println function comes from the Java “class library”  Ends with a semicolon (all statements do)

15 Compiling and Running Create the file HelloWorld.java in a text editor Compile:  javac HelloWorld.java Run:  java HelloWorld Output:  Hello World!

16 Comments Three kinds of comments: To simplify: comments are good // a one-line comment /* a multi-line comment */ /** a javadoc comment */

17 Reserved Words and Identifiers Reserved words are specified by the language  All Java reserved words are in the text Identifiers are specified by a programmer  Maybe you: e.g. HelloWorld  Maybe someone else: e.g. println

18 Restrictions and Conventions Restriction  Identifiers can not start with a digit Conventions  Title case for class names: HelloWorld  Uppercase for constants: MAX

19 White Space Conventions Idea: make programs easy to read Use consistent indentation Use blank lines and comments to visually separate methods The fact that it compiles doesn’t make it right…

20 Strong Typing Java is a “strongly typed” language All variables and values have a specific type Type is known when the program is compiled…. before it is run So all variables and values must be declared with a type before being used

21 Declaring Variables Syntax: Examples:  int count1, int count 2;  int count = 0;  String course1 = “CMPT 126”; ::=, …. ; ::= ::= =

22 Assignment We use the = operator for variable assignment  Initialization is a special case When a value is assigned, the old value is overwritten In Java, we use the final modifier to declare a variable constant  final int MAX_HEIGHT = 6;

23 Primitive Data Types in Java Four integer types:  byte, short, int, long Two floating point types  float, double One of them is for characters  char One of them is for boolean values  boolean

24 Expressions and Assignment An expression is a combination of one or more operators and operands Arithmetic operators: +, -, *, /, %  Use the normal order of operations e.g. int exp = 2 * 5 +7; count = count + 1; count++; Boolean operators: &&, ||

25 More Assignment Operators x += y is equivalent to x = x + y Also:  -=  *=  /=  %=

26 Data Conversion Non-matching types can be converted A widening conversion is automatic  e.g. from short to int A narrowing conversion may lose information  e.g. from float to int Three kinds of conversion:  Assignment  Promotion  Casting

27 Assignment Conversion final int dollars = 6; double money; money = dollars; Only works for widening conversion

28 Promotion int count = 2; float mass = ; mass = mass / count; Passing count to an operator that expects floating point values

29 Casting float mass = ; int roundedmass = (int) mass; Casting works for widening and narrowing  In this example, decimal part is just lost  Note: this does not actually round

30 Object Types The primitive types aren’t really enough Java also allows object types, or classes  Typically capitalized Object variables hold references to objects  The declaration only creates a reference This is different from primitive types  Variables of primitive type hold a value

31 Example: String Objects We have already seen one object type in Java: String A String object is a list of characters e.g. “Hello world!” or “My name is Aaron” Can be passed to print or println Can be concatenated using the (+) operator e.g. “Hello world! ” + “My name is Aaron” “I can also append numbers, like “ + 2

32 Object Instances We must create a new “instance” of an object to store something  Each object type has a constructor (more later)  Create instances using the reserved world new e.g. course = new String(“CMPT 126”); This creates a new String in memory  It stores the characters “CMPT 126”  The assignment sets course to refer to this instance

33 References and Instances String course; new String(“CMPT 126”) course = new String(“CMPT 126”); course: CMPT 126 course: