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

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
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.
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.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Software Technology - I Tools to do operations.....
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Lecture 3 Java Operators.
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Building Java Programs
Fundamentals 2.
Building Java Programs Chapter 2
Expressions and Assignment
elementary programming
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Building Java Programs Chapter 2
Building Java Programs
Presentation transcript:

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

Types Kinds of values that can be stored and manipulated boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text (“hello”, “example”).

Variables Named location that stores a value of one particular type Form: – TYPE NAME; Example: – String foo; – int x; A variable must be declared before it is used.

Java Identifiers An identifier is a name, such as the name of a variable. Identifiers may contain only –letters –digits (0 through 9) –the underscore character (_) –and the dollar sign symbol ($) which has a special meaning but the first character cannot be a digit.

Example identifiers: Check their correctness int k!34; int 2dfg; int test1; int test23we; int df_; int sd$; int $fg; int k.t; int k-t;

Java Identifiers, cont. Identifiers may not contain any spaces, dots (. ), asterisks ( * ), or other characters: 7-11 netscape.com util.* (not allowed) Identifiers can be arbitrarily long. Since Java is case sensitive, stuff, Stuff, and STUFF are different identifiers.

Keywords or Reserved Words Words such as if are called keywords or reserved words and have special, predefined meanings. Keywords cannot be used as identifiers. Other keywords: int, public, class

Primitive Types

Assignment An assignment statement is used to assign a value to a variable. answer = 42; Use ‘ = ‘ to give variables a value. Example: – String foo; – foo = “IAP 6.092”; Can be combined with variable declaration – String foo = “IAP 6.092”; int numberOfBaskets, eggsPerBasket; int numberOfBaskets=5, eggsPerBasket;

Operators Symbols that perform simple computations Assignment: = Addition: + Subtraction: - Multiplication: * Division: / Mod: %

Order of Operations Follows standard math rules: – Parentheses – Multiplication and division – Addition and subtraction – double x = 3 / 2 + 1; // x = 2.0 – double y = 3 / (2 + 1); // y = 1.0

Order of Operations – Cont.

The binary arithmetic operators *, /, and %, have lower precedence than the unary operators ++, --, and !, but have higher precedence than the binary arithmetic operators + and -. When binary operators have equal precedence, the operator on the left acts before the operator(s) on the right.

Sample Expressions

Increment (and Decrement) Operators Used to increase (or decrease) the value of a variable by 1 Easy to use, important to recognize The increment operator count++ or ++count The decrement operator count-- or --count

Increment (and Decrement) Operators equivalent operations count++; ++count; count = count + 1; count--; --count; count = count - 1;

Examples int k=0, y=0, x; x = ++k-y; System.out.println("x's value : "+x);; int k=0, y=0, x; x = k++-y; System.out.println("x's value : "+x);

Increment (and Decrement) Operators in Expressions after executing int m = 4; int result = 3 * (++m) result has a value of 15 and m has a value of 5 after executing int m = 4; int result = 3 * (m++) result has a value of 12 and m has a value of 5

Sample code: operators and assignments

Division Division (“/”) operates differently on integers and on doubles! Example: – double a = 5.0/2.0; // a = 2.5 – int b = 4/2; // b = 2 – int c = 5/2; // c = 2 – double d = 5/2; // d = 2.0

Conversion by casting int a = 2; // a = 2 double a = 2; // a = 2.0 (Implicit) int a = 18.7; // ERROR int a = (int)18.7; // a = 18 double a = 2/3; // a = 0.0 double a = (double)2/3; // a = …

Conversion by casting - Cont double z = 3.0/2.0; System.out.println("===== "+z); double t = 3/2; System.out.println("===== "+t); double m = (double)3/2; System.out.println("===== "+m);

Casting

Data Types and Their Relations in a Tree

Casting example public class Casting { public static void main(String[] args){ float a=12.5f; int i = (int) a; System.out.println("(int)12.5f==" + i); float f = i; System.out.println("float değeri: " + f); System.out.print(f); f =f * i; System.out.println("*" + i + "==" + f); } (int)12.5f==12 float değeri: *12==144.0

Which ones are correct? float f = 2.34f; double d = f; f=d; d=f; long a = 15878; f = 1.1*a; int a = 78; long b = a*9876; byte a = 126; int b = ++a; byte a; int b; a=b; byte a = 1; short b = a; float f = 2.34f; char c=65; double d; char c=65; d = c*f*1.5;

public class Casting_2 { public static void main(String args[]) { byte x = 126; System.out.println( DoIt(x) ); } static String DoIt(int a) { return "I've received an int of value "+a; } static String DoIt(byte a) { return "I've received a byte of value "+a; }

public class Casting_3 { public static void main(String args[]) { char x = 'A'; System.out.println( DoIt(x) ); } static String DoIt(int a) { return "I've received an int of value "+a; } static String DoIt(byte a) { return "I've received a byte of value "+a; }