CSI 3125, Preliminaries, page 1 Data Type, Variables.

Slides:



Advertisements
Similar presentations
Object Oriented Programming in JAVA
Advertisements

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.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
String Escape Sequences
Basic Elements of C++ Chapter 2.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Objectives You should be able to describe: Data Types
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
Names Variables Type Checking Strong Typing Type Compatibility 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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 Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
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.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Primitive Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
August 6, 2009 Data Types, Variables, and Arrays.
Primitive Variables.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Object Oriented Programming
Java package classes Java package classes.
Java - Data Types, Variables, and Arrays
Computers & Programming Languages
Statements and expressions - java
Variables and Constants
Presentation transcript:

CSI 3125, Preliminaries, page 1 Data Type, Variables

CSI 3125, Preliminaries, page 2 Java Is a Strongly Typed Language Every variable has a type Every expression has a type and Every type is strictly defined All assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. There are no automatic conversions of type The Java compiler checks all expressions and parameters to ensure that the types are compatible.

CSI 3125, Preliminaries, page 3 The Simple Types Java defines eight simple (or elemental) types of data: 1) byte 2) short 3) int 4) long 5) char 6) float 7) double 8) boolean. These can be put in four groups:

CSI 3125, Preliminaries, page 4 The Simple Types four groups ■ Integers : This group includes byte, short, int, and long, which are for whole valued signed numbers. ■ Floating-point numbers : This group includes float and double, which represent numbers with fractional precision. ■ Characters : This group includes char, which represents symbols in a character set, like letters and numbers. ■ Boolean : This group includes boolean, which is a special type for representing true/false values.

CSI 3125, Preliminaries, page 5 Integers : byte The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127. Variables of type byte are especially useful when working with a stream of data from a network or file. Byte variables are declared by the keyword byte. For example, byte b, c;

CSI 3125, Preliminaries, page 6 Integers : Short short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used Java type, since it is defined as having its high byte first. examples of short variable declarations: short s;

CSI 3125, Preliminaries, page 7 Integers : int The most commonly used integer type is int. It is a signed 32-bit type Has a range from –2,147,483,648 to 2,147,483,647. int are commonly employed to control loops and to index arrays. Example int a,b;

CSI 3125, Preliminaries, page 8 Floating-point numbers There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. Name Width in Bits Approximate Range double e–324 to 1.8e+308 float e−045 to 3.4e+038

CSI 3125, Preliminaries, page 9 Floating-point numbers : float The type float specifies a single-precision value that uses 32 bits of storage. float are useful when need a fractional component, but don’t require a large degree of precision. For example, float can be useful when representing dollars and cents. example float variable declarations: float a,b;

CSI 3125, Preliminaries, page 10 Floating-point numbers : double Double precision, as denoted by the double keyword, uses 64 bits to store a value. All mathematical functions, such as sin( ), cos( ), and sqrt( ), return double values. Need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice. Example double pi, r, a;

CSI 3125, Preliminaries, page 11 Character char, which represents symbols in a character set, like letters and numbers. Example Char a,b;

CSI 3125, Preliminaries, page 12 Booleans Java has a simple type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, such as a < b.

CSI 3125, Preliminaries, page 13 Variables The basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a scope, which defines their visibility, and a lifetime.

CSI 3125, Preliminaries, page 14 Declaring a Variable Syntax type identifier [ = value][, identifier [= value]...] ; type is one of Java’s data types, or the name of a class or interface. identifier is the name of the variable. initialize the variable by specifying an equal sign and a value. To declare more than one variable of the specified type, use a comma-separated list.

CSI 3125, Preliminaries, page 15 The Scope and Lifetime of Variables Variables declared inside a scope are not visible to code that is defined outside that scope. Thus, when declare a variable within a curly brace, are localizing that variable, protecting it from unauthorized access and/or modification.

CSI 3125, Preliminaries, page 16 The Type Promotion Rules First, all byte and short values are promoted to int. If one operand is a long, the whole expression is promoted to long. If one operand is a float, the entire expression is promoted to float. If any of the operands is double, the result is double.

CSI 3125, Preliminaries, page 17 Variables