You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Similar documents
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

Java Programming. Theory Manual

Jagannath Institute of Management Sciences Lajpat Nagar

Preview from Notesale.co.uk Page 9 of 108

Introduction to Programming Using Java (98-388)

Java is an Object Oriented Language. As a language that has the Object Oriented feature Java supports the following fundamental concepts:

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java Foundations Certified Junior Associate

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 6 Introduction to Defining Classes

CS 231 Data Structures and Algorithms, Fall 2016

CS260 Intro to Java & Android 03.Java Language Basics

Fundamentals of Programming Data Types & Methods

Java Basic Datatypees

OVERRIDING. 7/11/2015 Budditha Hettige 82

BASIC ELEMENTS OF A COMPUTER PROGRAM

Programming Language Concepts: Lecture 2

class objects instances Fields Constructors Methods static

Arrays Classes & Methods, Inheritance

C++ Objects Overloading Other C++ Peter Kristensen

Array. Prepared By - Rifat Shahriyar

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Objectives. Introduce static keyword examine syntax describe common uses

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

Object Orientation Fourth Story. Bok, Jong Soon

In Java there are three types of data values:

Java Identifiers, Data Types & Variables

Variables and Java vs C++

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Fundamentals of Programming Session 12

An Introduction to C++

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

CLASSES AND OBJECTS IN JAVA

Getting started with Java

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Short Notes of CS201

Binghamton University. CS-140 Fall Data Types in Java

Pointers II. Class 31

Java+- Language Reference Manual

CS201 - Introduction to Programming Glossary By

Abstract Data Types and Encapsulation Concepts

Pointers, Dynamic Data, and Reference Types

COE318 Lecture Notes Week 5 (Oct 3, 2011)

COP 3330 Final Exam Review

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Introduction To C#.NET

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Programming Language Concepts: Lecture 2

CMSC 4023 Chapter 11

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Class, Variable, Constructor, Object, Method Questions

Lecture 6 Introduction to Objects and Classes

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

1 Shyam sir JAVA Notes

Implementing Subprograms

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Object Oriented Programming with Java

Programming II (CS300)

Java Object Oriented Design. CSC207 Fall 2014

VARIABLES AND TYPES CITS1001

Declarations and Access Control SCJP tips

Chapter 11. Abstract Data Types and Encapsulation Concepts

Fundamental Concepts and Definitions

Java Professional Certificate Day 1- Bridge Session

Identifiers and Variables

CS201 Some Important Definitions

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Exam Duration: 2hrs and 30min Software Design

Lecture 10 Declarations and Scope

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Inheritance. Transitivity

C++ (classes) Hwansoo Han

OBJECT ORIENTED PROGRAMMING USING C++

COE318 Lecture Notes Week 6 (Oct 10, 2011)

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Chapter 2a Class Relationships

6.Introducing Classes 9. Exceptions

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Variables, Memory and Pointers

PROGRAMMING FUNDAMENTALS

9 Working with the Java Class Library

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

The Java language has a wide variety of modifiers, including the following:

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

CS201 Latest Solved MCQs

Chapter-8 DATA TYPES. Introduction. Variable:

Inheritance, and Polymorphism.

C11: Garbage Collection and Constructors

Contents. Chapter 1 Overview of the JavaScript C Engine...1. Chapter 2 JavaScript API Reference...23

Chief Reader Report on Student Responses:

Transcription:

Variable Types A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. You must declare all variables before they can be used. Following is the basic form of a variable declaration: data type variable [ = value][, variable [= value]...] ; Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list. Following are valid examples of variable declaration and initialization in Java: int a, b, c; // Declares three ints, a, b, and c. int a = 10, b = 10; // of initialization byte B = 22; // initializes a byte type variable B. double pi = 3.14159; // declares and assigns a value of PI. char a = 'a'; // the char variable a iis initialized with value 'a' This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java: Local variables Instance variables Class/Static variables Local Variables Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block. Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor, or block. Local variables are implemented at stack level internally. There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use. 1

Here, age is a local variable. This is defined inside pupage() method and its scope is limited to only this method. public class Test{ public void pupage(){ int age = 0; age = age + 7; System.out.println("Puppy age is : " + age); Test test = new Test(); test.pupage(); This will produce the following result: Puppy age is: 7 Following example uses age without initializing it, so it would give an error at the time of compilation. public class Test{ public void pupage(){ int age; age = age + 7; System.out.println("Puppy age is : " + age); Test test = new Test(); test.pupage(); 2

This will produce the following error while compiling it: Test.java:4:variable number might not have been initialized age = age + 7; ^ 1 error 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. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Instance variables can be declared in class level before or after use. Access modifiers can be given for instance variables. The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers. Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor. Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName. 3

import java.io.*; public class Employee{ // this instance variable is visible for any child class. public String name; // salary variable is visible in Employee class only. private double salary; // The name variable is assigned in the constructor. public Employee (String empname){ name = empname; // The salary variable is assigned a value. public void setsalary(double empsal){ salary = empsal; // This method prints the employee details. public void printemp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); Employee empone = new Employee("Ransika"); empone.setsalary(1000); empone.printemp(); 4

This will produce the following result: name : Ransika salary :1000.0 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 rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value. Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants. Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks. Static variables can be accessed by calling with the class name ClassName.VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables. 5

import java.io.*; public class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; salary = 1000; System.out.println(DEPARTMENT + "average salary:" + salary); This will produce the following result: Development average salary:1000 Note: If the variables are accessed from an outside class, the constant should be accessed as Employee.DEPARTMENT 6