Introduction to Java Primitive Types Operators Basic input and output.

Slides:



Advertisements
Similar presentations
Integer Arithmetic. Operator Priority Real Number Arithmetic.
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.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
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.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
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.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Mathematical Calculations in Java Mrs. G. Chapman.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
Mixing integer and floating point numbers in an arithmetic operation.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Chapter 2 Variables.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Mathematical Calculations in Java Mrs. C. Furman.
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[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
Objects First With Java A Practical Introduction Using BlueJ Casting Week
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
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.
THE DOUBLE VARIABLE The double variable can hold very large (or small) numbers. The maximum and minimum values are 17 followed by 307 zero’s.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 2. Program Construction in Java. 01 Java basics.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Elementary Programming
Chapter 4 – Fundamental Data Types
Nahla Abuel-ola / WIT.
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Explicit and Implicit Type Changes
OPERATORS (2) CSC 111.
Primitive Types Operators Basic input and output
Increment and Decrement
Unit-2 Objects and Classes
Numerical Data Types.
Arithmetic Expressions & Data Conversions
Chapter 2: Java Fundamentals
Java Programming Language
C Programming Pointers
Primitive Types and Expressions
Introduction to Java, and DrJava
Chapter 2: Java Fundamentals cont’d
Arithmetic Expressions & Data Conversions
Presentation transcript:

Introduction to Java Primitive Types Operators Basic input and output

Data Conversions What gets printed? What gets printed? –System.out.println(10 / 4.0 ); –System.out.println( ); –System.out.println( ); –System.out.println( * 4.0 ); Rules for math operators: Rules for math operators: –If BOTH operands are integers, then integer math is performed –Otherwise, floating point arithmetic is used

Data Conversions What happens if you try this in main? What happens if you try this in main? –int result = 10 / 4.0; –double result = 10 / 4; –double result = 10 % 3; –int result = 10 % 4.0;

Casting Rules If the conversion will lose data, Java will give an error If the conversion will lose data, Java will give an error –Going from 13.5 (double) to and int would lose information when 13.5 would become 13 –There is no problem however if you want to store 13 (int) into a double value because nothing is lost by 13.0 The way you tell Java you’re aware of the error and that it’s OK to compile is with a method called casting The way you tell Java you’re aware of the error and that it’s OK to compile is with a method called casting Casting changes the data type Casting changes the data type

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( i1 / (int)d2 );

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( d2 / (double)i2 );

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( d2 / (int)d4) );

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( (int)(d1 / d4) );

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( (double)(i1 / (int)d4) );

Casting Examples int i1 = 12; int i1 = 12; int i2 = 4; int i2 = 4; int i3 = 6; int i3 = 6; double d1 = 24.0; double d1 = 24.0; double d2 = 3.0; double d2 = 3.0; double d3 = 2.0; double d3 = 2.0; double d4 = 2.5; double d4 = 2.5; System.out.println( (double)i2 / i3 );

Casting exercises

Casting Exercises Cont’