Java Basics Data Types in Java.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
String Escape Sequences
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Chapter 2 Programming Building Blocks — Java Basics.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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 Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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.
Programming Languages Machine language Assembly language High-level languages The Java language.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Copyright Curt Hill Variables What are they? Why do we need them?
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming with Microsoft Visual Basic th Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
CHAPTER 4 GC 101 Identifiers and Data types. 2 IDENTIFIERS  identifier: A name given to a piece of data, method, etc.  Identifiers allow us to refer.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Fundamentals 2.
User Interaction and Variables
Java Basics Introduction to Java.
Identifiers - symbolic names
Lecture 2 Data Types Richard Gesick.
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
University of Central Florida COP 3330 Object Oriented Programming
Identifiers - symbolic names
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
IDENTIFIERS CSC 111.
Explicit and Implicit Type Changes
Introduction to Java Programming
Chapter 2 Edited by JJ Shepherd
Chapter 2: Java Fundamentals
elementary programming
Programming Building Blocks Java Basics
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
Java’s Central Casting
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Java Basics Data Types in Java

Data Types in Java Data type tells compiler: How much memory to allocate Format in which to store data Types of operations you will perform on data www.prolearninghub.com

Data Types in Java Java is a strongly typed language Data types may be Unlike C, Type checking is strictly enforced at run time Impossible to typecast incompatible types Data types may be Primitive data types Reference data types www.prolearninghub.com

Data Types in Java Primitive Numeric Integer int short long byte Floating point float double Non-numeric char boolean Non-primitive Arrays Classes Interfaces www.prolearninghub.com

Data Types in Java Primitive Data Types in Java Integer Data Types byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes) All numeric data types are signed The size of data types remain same on all platforms Char data type is 2 bytes as it uses the UNICODE character set. And so, Java supports internationalization Floating Data Types float (4 bytes) double (8 bytes) Character Data Types char (2 byte) Logical Data types boolean (1 bit) (true/false) www.prolearninghub.com

Integer Data Types in Java Size in bytes Minimum Value Maximum Value Byte 1 -128 127 Short 2 -32,768 32,767 Int 4 -2,147,483,648 2,147,483,647 long 8 -9,223,372,036,854,775,808 9,223,372,036,854,807 www.prolearninghub.com

Declaration Example int testGrade; int numPlayers, highScore, diceRoll; short xCoordinate, yCoordinate; byte ageInYears; long cityPopulation; www.prolearninghub.com

Floating Data Types in Java Size in bytes Minimum Values Maximum Values Float 4 1.4E-45 3.4028235E38 double 8 4.9E-324 1.7976931348623157E308 www.prolearninghub.com

Declaration Example float salesTax; double interestRate; double paycheck, sumSalaries; www.prolearninghub.com

Character Data Types in Java Size in bytes Minimum Values Maximum Values Char 2 Encoded as 0 Encoded as FFFF One Unicode Character (16 bits - 2 bytes) www.prolearninghub.com

Declaration Example char finalScore; char newline, tab, doubleQuotes; www.prolearninghub.com

Logical Data Types in Java boolean (1 bit) (true/false) Used for decision making or as "flag" variables www.prolearninghub.com

Declaration Example boolean isEmpty; boolean passed, failed; www.prolearninghub.com

Compatible Data Types Data Type Compatible Data Types byte byte short byte, short int byte, short, int, char long byte, short, int, long, char float float, byte, short, int, long, char double float, double, byte, short, int, long, char boolean boolean char char www.prolearninghub.com

Data Types in Java Variables Variables are defined in Java using the following format : <access modifier> <type> <variable name>; e.g. public String name; private int count; One value can be placed in one time in a variable but it may change syntax : dataType identifier; or dataType identifier1, identifier2, …; www.prolearninghub.com

Data Types in Java Variables A named storage in the computer’s memory that stores a value of a particular type for use by program Example of variable declaration: The data type can either be: built-in primitive types (e. g. int, double, char) Reference data types (e.g. String, BufferedReader) Naming Convention: Variable Name: First word lowercase & rest with their initials capitalized (Camel Casing) e.g. thisIsALongVariableName DataType VariableName int myAge, cellPhone ; double salary ; char tempChar ; www.prolearninghub.com

Data Types in Java Variables (Contd…) Using primitive data types is similar to other languages Variables can be declared anywhere in the program In Java, if a local variable is used without initializing it, the compiler will show an error int count ; int max=100; for (int count=0; count <max; count++) { int z = count * 10; } BEST PRACTICE: Declare a variable in program only when required Do not declare variables upfront like in C www.prolearninghub.com

Variable Naming Convention First letter is lowercase Embedded words begin with uppercase letter Name of the variable should be reasonable and meaningful that depict the data store. Avoid extra large variable name. Similar Java keyword should not be used as variable name. www.prolearninghub.com

Assign Value to Variable Variables Assignment operator = Value on the right of the operator is assigned to the variable on the left Value on the right can be a literal (text representing a specific value), another variable, or an expression (explained later) Syntax: dataType variableName = initialValue; Or dataType variable1 = initialValue1, variable2 = initialValue2, …; www.prolearninghub.com

Assign Value to Object Variables Assignment operator = Value on the right of the operator is assigned to the variable on the left Value on the right can be a literal (text representing a specific value), another variable, or an expression (explained later) Syntax: dataType variableName = initialValue; Or dataType variable1 = initialValue1, variable2 = initialValue2, …; www.prolearninghub.com

Data Types in Java Comments in Java A single line comment in Java starts with // A multi line comment starts with /* & ends with */ // This is a single line comment in Java /* This is a multi line comment in Java */ www.prolearninghub.com

Data Types in Java Reference Data Types Hold the reference of dynamically created objects which are in the heap Can hold three kinds of values: Class type: Points to an object / class instance Interface type: Points to an object which is implementing the corresponding interface Array type: Points to an array instance or “null” Difference between Primitive & Reference data types: Primitive data types hold values themselves Reference data types hold reference to objects, i.e. they are not objects, but reference to objects Objects & Arrays are accessed using reference variables in Java A reference variable Is similar to a pointer (stores memory address of an object) Java does not support the explicit use of addresses like other languages www.prolearninghub.com

Data Types in Java Reference Data Types (Contd…) Java does not allow pointer manipulation or pointer arithmetic Memory Representation: primitive reference A reference type cannot be cast to a primitive type A reference type can be assigned ‘null- to show that it is not referring to any int primitive = 5; String reference = “Hello” ; 5 Hello www.prolearninghub.com

Data Types in Java Typecasting Primitive Data Types Automatic type changing is known as Implicit Conversion - A variable of smaller capacity can be assigned to another variable of bigger capacity Whenever a larger type is converted to a smaller type, we have to explicitly specify the type cast operator This prevents any accidental loss of data int i = 10; double d; d = i; double d = 10; d = i; i = (int) d; Type cast operator () www.prolearninghub.com

Access Modifier A modifier is a keyword placed in a class, method or variable declaration that changes how it operates. There are basically four types of access modifier in JAVA public private default protected www.prolearninghub.com

Default Modifier If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes. www.prolearninghub.com

Public Modifier A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. The following function uses public access modifier: Example : public static void main(String[] arguments) { / // ... } www.prolearninghub.com

Private Modifier The private modifier specifies that the member can only be accessed in its own class. Example: public class football{ private String score; public String getScore() { return this.score; } public void setScore(String score) { this.format = format; www.prolearninghub.com

Protected Modifier The protected modifier specifies that the member can only be accessed within its own package (as with package-private). Example: class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) { www.prolearninghub.com

Protected Modifier Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, that's why we used protected modifier. www.prolearninghub.com

Sample Exercise #1 Is it legal to assign a char to an int variable ? Yes, it is possible int i = ‘a’; // assigns 97 to I www.prolearninghub.com

Sample Exercise #2 Is it legal to assign an int to an char variable ? Yes, it is possible char c = 97; // assigns ‘a’ to c www.prolearninghub.com

Sample Exercise #3 It is possible to perform arithmetic on char variables ? Yes, it is possible char ch = ‘a’; ch = ch + 1; // assigns ‘b’ to ch www.prolearninghub.com

Sample Exercise #4 Does a int value can be assigned to a char type variable ? Yes, we can assign a integer value to a char type variable but a char type value cannot be assigned to a int type variable www.prolearninghub.com