J AVA AND V ARIABLES. O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Written by: Dr. JJ Shepherd
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
CSC – Java Programming II Lecture 9 January 30, 2002.
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.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
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.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
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.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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.
CORE JAVA C LASS Class is a template for multiple objects with similar features and It is a blue print for objects. It defines.
Primitive Variables.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
 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.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
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.
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++
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
In Java.... Variables contain the values of primitive data types Value Types.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
1.2 Primitive Data Types and Variables
1 MATERI PENDUKUNG TIPE DATA Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
 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 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.
Quick Summary C++/CLI Basics Data Types Controls Arrays In-class assignments.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
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.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Java Programming Language Lecture27- An Introduction.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Objects as a programming concept
JAVA MULTIPLE CHOICE QUESTION.
Java package classes Java package classes.
null, true, and false are also reserved.
Computers & Programming Languages
Introduction to Java Programming
An overview of Java, Data types and variables
Module 2 - Part 1 Variables, Assignment, and Data Types
Java Programming Language
Names of variables, functions, classes
Chap 2. Identifiers, Keywords, and Types
Variables and Constants
Presentation transcript:

J AVA AND V ARIABLES

O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage Collection Arrays

D ECLARING A V ARIABLE Variables can be broken into two groups Primitives Object references Primitives contain single values Object references allow us to touch object instances

D ECLARING A V ARIABLE Just like in C, Java needs you to set the type of each variable Primitive types have the same names as in C but different sizes Java does not have an unsigned type To put a bigger primitive into a smaller primitive, use typecasting int i = (int)12.5f;

P RIMITIVE T YPES boolean – true or false char – 16 bits Unicode not ASCII byte – 8 bits -128 to 127 short – 16 bits -32,768 to int – 32 bits -2,147,483,648 to 2,147,483,647

J AVA K EYWORDS Just like C, Java has special words you can not use for variables Eg. throw, package, break, while, final, default, long, short, super, this, enum, catch, etc...

R EFERENCE V ARIABLES When an object is declared, it is a reference to any object not the object itself A good analogy would be it’s a pointer You must use the ‘dot’ (. ) operator when working with and object reference variable E.g. myObject.dosomething() myObject.somevariable

O BJECT D ECLARATION AND A SSIGNMENT The statement Dog mydog = new Dog(); works in 3 steps: A new reference variable (mydog) is defined Space is made in memory for a Dog object The mydog object reference is set to point to the memory where the Dog object was placed

O BJECTS AND G ARBAGE C OLLECTION Since object reference variables only ‘pointer’ to objects, we can change which object they point to easily If an object does not have at least on object reference variable, it is lost and will get destroyed

A RRAYS Arrays are objects in Java, hence int[] nums; // not enough nums = new int[7]; // is also needed Other than the definition, they work remarkably like arrays in C The first element is ‘0’ You use [] to access different elements

S IMPLE E XAMPLE Using the code on the web, add a 3 rd cat to the cat array and print out its information Add a new piece of information to all cats and print it out also