“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Chapter 2: Elementary Programming Shahriar Hossain.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
String Escape Sequences
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.
Mixing integer and floating point numbers in an arithmetic operation.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
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.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Escape Sequences What if we wanted to print the quote character?
OPERATORS (2) CSC 111.
Principles of Computer Programming (using Java) Chapter 2, Part 1
Examples of Primitive Values
Chapter 2: Basic Elements of Java
Java’s Central Casting
Presentation transcript:

“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna Harrison Thought for the Day

Creating an Object (cont.) f is called a reference variable new creates a new object (instantiation) Factorial f = new Factorial(); f Factorial int fact (int n)

Using the Object f.fact(7) Calls the fact method in the object referred to by f f Factorial int fact (int n)

Printing Multiple Values public class Program3 { public static void main (String[] args) //===================================== { Factorial f = new Factorial(); System.out.println("Factorial of 7 = " + f.fact(7)); } // main } // class Program3 + used for string concatenation Numbers, etc. automatically converted to strings

Chapter 2 Types, Values, Variables and Expressions

Primitive Data Types Java has eight primitive data types: – int – double – byte, short, long – float – char – boolean

The int Type Similar to int in Python Signed integer values from – to (32 bits) E.g. – int k = ; – int m = -7;

The double Type Similar to float in Python Values from – E+308 to E+308 E.g. – double d = 57.64; – double e = -7.23E-5;

Strings Are objects (not primitive data types) Similar to the str type in Python Escape sequences: begin with backslash \ E.g. – String s = ”George”; – String t = ”He said \”Hi\”.”; – String f = ”C:\\Windows”;

Characters The char type E.g. – char ch = ’a’; – ch = ’?’; – ch = ’\n’;

The boolean Type Similar to the bool type in Python Only two possible values: – true – false E.g. – boolean b = false; – b = (x <= 10);

Operators Arithmetic: – + - * / –Mod (remainder): % E.g. – x = 2 * a + b; – y = x % 3;

Operators (cont.) Relational – == != – >= Logical –and: && –or: || –not: !

Operators (cont.) E.g. – if (x != 0)... – if (ch >= ’a’ && ch <= ’z’)... – if (! s.equals(”George”))...

Expression Compatibility Types may be mixed in expressions “Smaller” types are automatically promoted to “bigger” types int k, j; double d, e;... System.out.println(d + k); // OK d = k; // OK j = e; // Not OK!

Explicit Type Conversion How do we force the compiler to allow demotion? Use a cast (or type-cast) – (type)expression int j; double e;... j = (int)e; // OK Like j = int(e) in Python

Type Casting (cont.) int j, k; double d = 3.9, e = 2E20; j = (int)d; // OK k = (int)e; // Too big System.out.println("j = " + j); System.out.println("k = " + k); j = 3 k =

Assignments Unusually, assignment in Java is an expression, not a statement So, has a value! a = 3 int i, j, k; i = ( j = (k = 0)); i = j = k = 0; // Brackets not needed