Java Data Types.

Slides:



Advertisements
Similar presentations
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
JAVA PROGRAM Variable, Datatype & Operators. In this page, we will learn about the variable and java data types. Variable is a name of memory location.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C Tokens Identifiers Keywords Constants Operators 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 Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
Primitive Variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
August 6, 2009 Data Types, Variables, and Arrays.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Department of Computer Engineering Working With Data Computer Programming for International Engineers.
Chapter One Lesson Three DATA TYPES ©
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Computing with C# and the.NET Framework Chapter 4 More Control Structures and Types ©2003, 2011 Art Gittleman.
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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: รัฐภูมิ เถื่อนถนอม
Java Tokens Token A java program contains numbers of classes,
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
Yanal Alahmad Java Workshop Yanal Alahmad
Selenium WebDriver Web Test Tool Training
University of Central Florida COP 3330 Object Oriented Programming
Java package classes Java package classes.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
Primitive and Reference Data Values
An overview of Java, Data types and variables
Chapter 2: Java Fundamentals
Recap Week 2 and 3.
Unit 3: Variables in Java
Names of variables, functions, classes
Presentation transcript:

Java Data Types

Data Types in Java Data Types Non primitive primitive Character Numeric Boolean Floating-point Integer float double boolean char byte short int long

Data Types in Java byte: 8-bit signed two's complement integer. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7 -1) Default value is 0 Used to save space in large arrays, mainly in place of integers Example: byte a = 100 , byte b = -50

Data Types in Java short: 16-bit signed two's complement integer. Minimum value is -32,768 (-2^15) Maximum value is 32,767 (inclusive) (2^15 -1) Used to save memory Default value is 0. Example: short s = 10000, short r = -20000

Data Types in Java int: 32-bit. Minimum value is -2^31 Maximum value is (2^31 -1) Generally used as the default data type for integral values unless there is a concern about memory. The default value is 0. Example: int a = 100000, int b = -200000

Data Types in Java long: 64-bit Minimum value is (-2^63) Maximum value is (2^63 -1) This type is used when a wider range than int is needed. Exam: light distance per year Default value is 0L. Example: long a = 100000L, long b = -200000L

Data Types - long class Light { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; lightspeed = 186000L; days = 1000L; seconds = days * 24 * 60 * 60; distance = lightspeed * seconds; System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles."); }

Data Types in Java float: 32-bit. Default value is 0.0f. Example: float f1 = 234.5f

Data Types in Java double: 64-bit. Default value is 0.0d. Example: double d1 = 123.4 As a decimal value multiplied by a power of 10. You can do this in Java by writing the number as a decimal value followed by an E, or an e, preceding the power of 10 that you require. For example,149,600,000 can be written as 1.496E8.

Data Types in Java boolean: Represents one bit of information. Two possible values: true and false. Default value is false. Example: boolean one = true

Data Types in Java char: 16-bit Unicode character. Minimum value is '\u0000' (or 0). Maximum value is '\uffff' (or 65,535 inclusive). Char data type is used to store any character. Example: char letterA ='A'

Data Types Example public class AverageFruit { OUTPUT: public static void main(String[] args) { // Declare and initialize three variables double numOranges = 50.0E-1; // Initial value is 5.0 double numApples = 1.0E1; // Initial value is 10.0 double averageFruit = 0.0; averageFruit = (numOranges + numApples)/2.0; System.out.println(“A totally fruity program”); System.out.println(“Average fruit is “ + averageFruit); } OUTPUT: A totally fruity program Average fruit is 7.5

Data Types in Java Data Type Default Value Default size General Value boolean false 1 bit true, false char '\u0000' 2 byte A, b ... byte 1 byte 100 short 2 byte (215-1) 500 int 4 byte long 0L 8 byte float 0.0f 2.1 double 0.0d

Data Types in Java Category Types Size (bits) Minimum Value Maximum Value Precision Example Integer byte 8 -128 127 From +127 to -128 byte b = 65; char 16 216-1 All Unicode characters char c = 'A'; char c = 65; short -215 215-1 From +32,767 to -32,768 short s = 65; int 32 -231 231-1 From +2,147,483,647 to -2,147,483,648 int i = 65; long 64 -263 263-1 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 long l = 65L; Floating-point float 2-149 (2-2-23)·2127 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f; double 2-1074 (2-2-52)·21023 From 1.797,693,134,862,315,7 E+308 to 4.9 E-324 double d = 65.55; Other boolean 1 -- false, true boolean b = true;

Java Program Write a program that display every data type data value.

Reserved words in Java abstract assert boolean break byte case catch char class const* continue default double do else enum extends false final finally float for goto* if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

special escape sequences Notation Character represented \n Newline (0x0a) \r Carriage return (0x0d) \f Formfeed (0x0c) \b Backspace (0x08) \s Space (0x20) \t tab \" Double quote \' Single quote \\ backslash \ddd Octal character (ddd) \uxxxx Hexadecimal UNICODE character (xxxx)

Variable Variable is name of reserved area allocated in memory. int vData=50;

Types of Variable There are three types of variables in java local variable instance variable Class/static variable

Types of variables class A { } //end of class {  int data=50; //instance variable   static int m=100; //static variable   void method()  int n=90; //local variable   }   } //end of class 

Local variables Declared in methods or blocks Destroyed once it exits the method or block Access modifiers cannot be used Visible only within the declared method or block There is no default value for local variables so should initialized assigned before the first use.

Local variables public class Test{ public void pupAge(){ int age = 0; // Test without initialized age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]){ Test objTest = new Test(); objTest.pupAge();

Instance variables Instance variables are declared in a class, but outside a method, constructor or any block When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed Access modifiers can be given for instance variables. Instance variables have default values

Instance variables public class Employee{ public String name; private double salary; public Employee (String empName){ name = empName; } public void setSalary(double empSal){ salary = empSal; public void printEmp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); public static void main(String args[]){ Employee empOne = new Employee("Ransika"); empOne.setSalary(1000); empOne.printEmp();

Class/static variables Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables are created when the program starts and destroyed when the program stops. have default values.

Class/static variables public class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT + "average salary:" + salary); }

Mathematical functions Math.abs(-1); Math.floor(1.9); Math.ceil(1.1); java.lang.Math.sqrt(4); Math.PI; Math.pow(5, 2); Math.random()

Mathematical functions import java.lang.Math; public class Program { public static void main(String[] args) { // This version uses an int. int value = Math.abs(-1); System.out.println(value); // This version uses a double. double value2 = Math.abs(-1.23); System.out.println(value2); int value3 = Math.abs(1); System.out.println(value3); } Output 1 1.23

Mathematical functions import java.lang.Math; public class Program { public static void main(String[] args) { // These are all reduced, even the negative number. double floor1 = Math.floor(1.9); double floor2 = Math.floor(1.1); double floor3 = Math.floor(-1.3); System.out.println(floor1); System.out.println(floor2); System.out.println(floor3); } Output 1.0 -2.0

Programs for practics Variable Sum of two integers * / %

Java Programs