SlideShare a Scribd company logo
Data Types, Variables, Arrays
and Operators
Theandroid-mania.com
What are Variables?
 Variables are the nouns of a programming language-that
is, they are the entities (values and data) that act or are
acted upon.
 A variable declaration always contains two components:
the type of the variable and its name. The location of the
variable declaration, that is, where the declaration
appears in relation to other code elements, determines its
scope.
Theandroid-mania.com
What are Data-Types
 All variables in the Java language must have a data type. A
variable's data type determines the values that the variable
can contain and the operations that can be performed on it.
For example, the declaration
int count
declares that count is an integer (int). Integers can contain
only integral values (both positive and negative), and you
can use the standard arithmetic operators (+, -, *, and/) on
integers to perform the standard arithmetic operations
(addition, subtraction, multiplication, and division,
respectively).
Theandroid-mania.com
Data-Types
 There are two major categories of data types in the Java
language: primitive and reference.
The following table lists, by keyword, all of the primitive
data types supported by Java, their sizes and formats, and
a brief description of each.
Theandroid-mania.com
Variables
 Instance Variables (Non-Static Fields) Technically speaking, objects store 
their individual states in "non-static fields", that is, fields declared without 
the static keyword. Non-static fields are also known as instance
variables because their values are unique to eachinstance of a class (to each 
object, in other words); the currentSpeed of one bicycle is independent from 
the currentSpeed of another.
 Class Variables (Static Fields) A class variable is any field declared with 
the static modifier; this tells the compiler that there is exactly one copy of 
this variable in existence, regardless of how many times the class has been 
instantiated. A field defining the number of gears for a particular kind of 
bicycle could be marked as static since conceptually the same number of 
gears will apply to all instances. The code static int numGears = 6; would 
create such a static field. Additionally, the keyword final could be added to 
indicate that the number of gears will never change.
Theandroid-mania.com
 Local Variables Similar to how an object stores its state in fields, a method 
will often store its temporary state in local variables. The syntax for declaring 
a local variable is similar to declaring a field (for example, int count = 0;). 
There is no special keyword designating a variable as local; that 
determination comes entirely from the location in which the variable is 
declared — which is between the opening and closing braces of a method. 
As such, local variables are only visible to the methods in which they are 
declared; they are not accessible from the rest of the class.
 Parameters You've already seen examples of parameters, both in 
the Bicycle class and in the main method of the "Hello World!" application. 
Recall that the signature for the main method is public static void 
main(String[] args). Here, the args variable is the parameter to this method. 
The important thing to remember is that parameters are always classified as 
"variables" not "fields". This applies to other parameter-accepting constructs 
as well (such as constructors and exception handlers) that you'll learn about 
later in the tutorial.
Theandroid-mania.com
Naming
 The rules and conventions for naming your variables can be 
summarized as follows:
Variable names are case-sensitive. A variable's name can be any legal 
identifier — an unlimited-length sequence of Unicode letters and digits, 
beginning with a letter, the dollar sign "$", or the underscore character "_". 
The convention, however, is to always begin your variable names with a 
letter, not "$" or "_". Additionally, the dollar sign character, by convention, is 
never used at all. You may find some situations where auto-generated 
names will contain the dollar sign, but your variable names should always 
avoid using it. A similar convention exists for the underscore character; while 
it's technically legal to begin your variable's name with "_", this practice is 
discouraged. White space is not permitted.
Theandroid-mania.com
 The Java programming language is statically-typed, which means that all 
variables must first be declared before they can be used. This involves 
stating the variable's type and name, as you've already seen:
int gear = 1;
Doing so tells your program that a field named "gear" exists, holds numerical 
data, and has an initial value of "1". A variable's data type determines the 
values it may contain, plus the operations that may be performed on it. In 
addition to int, the Java programming language supports seven 
other primitive data types. A primitive type is predefined by the language 
and is named by a reserved keyword. Primitive values do not share state 
with other primitive values. The eight primitive data types supported by the 
Java programming language are:
Theandroid-mania.com
 byte: The byte data type is an 8-bit signed two's complement integer. It has
a minimum value of -128 and a maximum value of 127 (inclusive).
The byte data type can be useful for saving memory in large arrays, where
the memory savings actually matters. They can also be used in place
of int where their limits help to clarify your code; the fact that a variable's
range is limited can serve as a form of documentation.
 short: The short data type is a 16-bit signed two's complement integer. It has
a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As
with byte, the same guidelines apply: you can use a short to save memory in
large arrays, in situations where the memory savings actually matters.
 int: The int data type is a 32-bit signed two's complement integer. It has a
minimum value of -2,147,483,648 and a maximum value of 2,147,483,647
(inclusive). For integral values, this data type is generally the default choice
unless there is a reason (like the above) to choose something else. This data
type will most likely be large enough for the numbers your program will use,
but if you need a wider range of values, use long instead.
Theandroid-mania.com
 Subsequent characters may be letters, digits, dollar signs, or underscore
characters. Conventions (and common sense) apply to this rule as well.
When choosing a name for your variables, use full words instead of cryptic
abbreviations. Doing so will make your code easier to read and understand.
In many cases it will also make your code self-documenting; fields
named cadence, speed, and gear, for example, are much more intuitive than
abbreviated versions, such as s, c, and g. Also keep in mind that the name
you choose must not be a keyword or reserved word.
 If the name you choose consists of only one word, spell that word in all
lowercase letters. If it consists of more than one word, capitalize the first
letter of each subsequent word. The names gearRatio and currentGear are
prime examples of this convention. If your variable stores a constant value,
such as static final int NUM_GEARS = 6, the convention changes slightly,
capitalizing every letter and separating subsequent words with the
underscore character. By convention, the underscore character is never used
elsewhere.
Theandroid-mania.com
 byte: The byte data type is an 8-bit signed two's complement integer. It has
a minimum value of -128 and a maximum value of 127 (inclusive).
The byte data type can be useful for saving memory in large arrays, where
the memory savings actually matters. They can also be used in place
of int where their limits help to clarify your code; the fact that a variable's
range is limited can serve as a form of documentation.
 short: The short data type is a 16-bit signed two's complement integer. It has
a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As
with byte, the same guidelines apply: you can use a short to save memory in
large arrays, in situations where the memory savings actually matters.
 int: The int data type is a 32-bit signed two's complement integer. It has a
minimum value of -2,147,483,648 and a maximum value of 2,147,483,647
(inclusive). For integral values, this data type is generally the default choice
unless there is a reason (like the above) to choose something else. This data
type will most likely be large enough for the numbers your program will use,
but if you need a wider range of values, use long instead.
Theandroid-mania.com
 long: The long data type is a 64-bit signed two's complement integer. It has a
minimum value of -9,223,372,036,854,775,808 and a maximum value of
9,223,372,036,854,775,807 (inclusive). Use this data type when you need a
range of values wider than those provided by int.
 float: The float data type is a single-precision 32-bit IEEE 754 floating point.
Its range of values is beyond the scope of this discussion, but is specified in
the Floating-Point Types, Formats, and Values section of the Java Language
Specification. As with the recommendations for byte and short, use
a float (instead of double) if you need to save memory in large arrays of
floating point numbers. This data type should never be used for precise
values, such as currency. For that, you will need to use
the java.math.BigDecimal class instead. Numbers and
Strings covers BigDecimal and other useful classes provided by the Java
platform.
Theandroid-mania.com
 double: The double data type is a double-precision 64-bit IEEE 754 floating
point. Its range of values is beyond the scope of this discussion, but is
specified in the Floating-Point Types, Formats, and Values section of the Java
Language Specification. For decimal values, this data type is generally the
default choice. As mentioned above, this data type should never be used for
precise values, such as currency.
 boolean: The boolean data type has only two possible values: true and false.
Use this data type for simple flags that track true/false conditions. This data
type represents one bit of information, but its "size" isn't something that's
precisely defined.
 char: The char data type is a single 16-bit Unicode character. It has a
minimum value of 'u0000' (or 0) and a maximum value of 'uffff'(or 65,535
inclusive).
Theandroid-mania.com
Arrays
 An array is a container object that holds a fixed number of values of a single
type. The length of an array is established when the array is created. After
creation, its length is fixed. You've seen an example of arrays already, in
the main method of the "Hello World!" application. This section discusses
arrays in greater detail.
Each item in an array is called an element, and each element is
accessed by its numerical index
for ex:
declares an array of integers
int[] anArray;
allocates memory for 10 integers
anArray = new int[10];
Theandroid-mania.com
initialize elements
anArray[index] = value;
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
Theandroid-mania.com

More Related Content

PPTX
Data types
PPTX
Data types
PPSX
Data type
PPTX
Concept Of C++ Data Types
 
PPTX
Data Types
PPTX
Computer data type and Terminologies
PDF
TEXT PLAGIARISM CHECKER USING FRIENDSHIP GRAPHS
PPTX
Data types
Data types
Data type
Concept Of C++ Data Types
 
Data Types
Computer data type and Terminologies
TEXT PLAGIARISM CHECKER USING FRIENDSHIP GRAPHS

What's hot (20)

PPTX
DATATYPE IN C# CSHARP.net
PDF
G04124041046
PDF
NLP - Sentiment Analysis
PDF
Revision notes for exam 2011 computer science with C++
PDF
Introduction to Text Mining
PPTX
java programming basics - part ii
PDF
Python 3.x quick syntax guide
PDF
A Text Mining Research Based on LDA Topic Modelling
PDF
Text Steganography Using Compression and Random Number Generators
PDF
Simulation and Performance Analysis of Long Term Evolution (LTE) Cellular Net...
PDF
SULTHAN's - Data Structures
PDF
Sienna 12 huffman
DOCX
Oo ps exam answer2
PPTX
PPTX
PDF
Topic Modeling - NLP
PDF
Text Mining Analytics 101
PDF
IJERD (www.ijerd.com) International Journal of Engineering Research and Devel...
PPT
I- Extended Databases
PDF
Binary Trees
DATATYPE IN C# CSHARP.net
G04124041046
NLP - Sentiment Analysis
Revision notes for exam 2011 computer science with C++
Introduction to Text Mining
java programming basics - part ii
Python 3.x quick syntax guide
A Text Mining Research Based on LDA Topic Modelling
Text Steganography Using Compression and Random Number Generators
Simulation and Performance Analysis of Long Term Evolution (LTE) Cellular Net...
SULTHAN's - Data Structures
Sienna 12 huffman
Oo ps exam answer2
Topic Modeling - NLP
Text Mining Analytics 101
IJERD (www.ijerd.com) International Journal of Engineering Research and Devel...
I- Extended Databases
Binary Trees
Ad

Viewers also liked (6)

PPTX
Array,data type
PPT
Data Structures- Part3 arrays and searching algorithms
PPTX
Data array and frequency distribution
PPSX
frequency distribution table
PPTX
Frequency Distributions and Graphs
PPTX
Chapter 2: Frequency Distribution and Graphs
Array,data type
Data Structures- Part3 arrays and searching algorithms
Data array and frequency distribution
frequency distribution table
Frequency Distributions and Graphs
Chapter 2: Frequency Distribution and Graphs
Ad

Similar to Data types ,variables,array (20)

PPTX
JAVA LESSON-01.pptx
PPSX
Java session3
PPTX
Computer Programming Java Data Types.pptx
PPTX
a variable in Java must be a specified data type
PPTX
Java - Basic Datatypes.pptx
PPTX
PPTX
5. variables & data types
PPTX
DATA TYPES in Python done by Sanajai of MCA
PDF
7-Java Language Basics Part1
PPTX
java Basic Programming Needs
PPTX
Computer programming 2 Lesson 5
PPTX
OOP-java-variables.pptx
PDF
Java data types, variables and jvm
PPTX
Java Foundations: Data Types and Type Conversion
PDF
CIS 1403 Lab 2- Data Types and Variables
PDF
Java basic datatypes
PPTX
L2 datatypes and variables
PPTX
Identifiers, keywords and types
PPTX
DATATYPES IN JAVA primitive and nonprimitive.pptx
PPTX
Data types in java.pptx power point of java
JAVA LESSON-01.pptx
Java session3
Computer Programming Java Data Types.pptx
a variable in Java must be a specified data type
Java - Basic Datatypes.pptx
5. variables & data types
DATA TYPES in Python done by Sanajai of MCA
7-Java Language Basics Part1
java Basic Programming Needs
Computer programming 2 Lesson 5
OOP-java-variables.pptx
Java data types, variables and jvm
Java Foundations: Data Types and Type Conversion
CIS 1403 Lab 2- Data Types and Variables
Java basic datatypes
L2 datatypes and variables
Identifiers, keywords and types
DATATYPES IN JAVA primitive and nonprimitive.pptx
Data types in java.pptx power point of java

More from Gujarat Technological University (6)

PPSX
PPSX
Object Oriented Programing and JAVA
PPS
XML - The Extensible Markup Language
PPS
Creating classes and applications in java
Object Oriented Programing and JAVA
XML - The Extensible Markup Language
Creating classes and applications in java

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Computing-Curriculum for Schools in Ghana
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
GDM (1) (1).pptx small presentation for students
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Computing-Curriculum for Schools in Ghana

Data types ,variables,array

  • 1. Data Types, Variables, Arrays and Operators Theandroid-mania.com
  • 2. What are Variables?  Variables are the nouns of a programming language-that is, they are the entities (values and data) that act or are acted upon.  A variable declaration always contains two components: the type of the variable and its name. The location of the variable declaration, that is, where the declaration appears in relation to other code elements, determines its scope. Theandroid-mania.com
  • 3. What are Data-Types  All variables in the Java language must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. For example, the declaration int count declares that count is an integer (int). Integers can contain only integral values (both positive and negative), and you can use the standard arithmetic operators (+, -, *, and/) on integers to perform the standard arithmetic operations (addition, subtraction, multiplication, and division, respectively). Theandroid-mania.com
  • 4. Data-Types  There are two major categories of data types in the Java language: primitive and reference. The following table lists, by keyword, all of the primitive data types supported by Java, their sizes and formats, and a brief description of each. Theandroid-mania.com
  • 5. Variables  Instance Variables (Non-Static Fields) Technically speaking, objects store  their individual states in "non-static fields", that is, fields declared without  the static keyword. Non-static fields are also known as instance variables because their values are unique to eachinstance of a class (to each  object, in other words); the currentSpeed of one bicycle is independent from  the currentSpeed of another.  Class Variables (Static Fields) A class variable is any field declared with  the static modifier; this tells the compiler that there is exactly one copy of  this variable in existence, regardless of how many times the class has been  instantiated. A field defining the number of gears for a particular kind of  bicycle could be marked as static since conceptually the same number of  gears will apply to all instances. The code static int numGears = 6; would  create such a static field. Additionally, the keyword final could be added to  indicate that the number of gears will never change. Theandroid-mania.com
  • 6.  Local Variables Similar to how an object stores its state in fields, a method  will often store its temporary state in local variables. The syntax for declaring  a local variable is similar to declaring a field (for example, int count = 0;).  There is no special keyword designating a variable as local; that  determination comes entirely from the location in which the variable is  declared — which is between the opening and closing braces of a method.  As such, local variables are only visible to the methods in which they are  declared; they are not accessible from the rest of the class.  Parameters You've already seen examples of parameters, both in  the Bicycle class and in the main method of the "Hello World!" application.  Recall that the signature for the main method is public static void  main(String[] args). Here, the args variable is the parameter to this method.  The important thing to remember is that parameters are always classified as  "variables" not "fields". This applies to other parameter-accepting constructs  as well (such as constructors and exception handlers) that you'll learn about  later in the tutorial. Theandroid-mania.com
  • 7. Naming  The rules and conventions for naming your variables can be  summarized as follows: Variable names are case-sensitive. A variable's name can be any legal  identifier — an unlimited-length sequence of Unicode letters and digits,  beginning with a letter, the dollar sign "$", or the underscore character "_".  The convention, however, is to always begin your variable names with a  letter, not "$" or "_". Additionally, the dollar sign character, by convention, is  never used at all. You may find some situations where auto-generated  names will contain the dollar sign, but your variable names should always  avoid using it. A similar convention exists for the underscore character; while  it's technically legal to begin your variable's name with "_", this practice is  discouraged. White space is not permitted. Theandroid-mania.com
  • 8.  The Java programming language is statically-typed, which means that all  variables must first be declared before they can be used. This involves  stating the variable's type and name, as you've already seen: int gear = 1; Doing so tells your program that a field named "gear" exists, holds numerical  data, and has an initial value of "1". A variable's data type determines the  values it may contain, plus the operations that may be performed on it. In  addition to int, the Java programming language supports seven  other primitive data types. A primitive type is predefined by the language  and is named by a reserved keyword. Primitive values do not share state  with other primitive values. The eight primitive data types supported by the  Java programming language are: Theandroid-mania.com
  • 9.  byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.  short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.  int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead. Theandroid-mania.com
  • 10.  Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.  If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere. Theandroid-mania.com
  • 11.  byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.  short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.  int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead. Theandroid-mania.com
  • 12.  long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.  float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform. Theandroid-mania.com
  • 13.  double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.  boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.  char: The char data type is a single 16-bit Unicode character. It has a minimum value of 'u0000' (or 0) and a maximum value of 'uffff'(or 65,535 inclusive). Theandroid-mania.com
  • 14. Arrays  An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail. Each item in an array is called an element, and each element is accessed by its numerical index for ex: declares an array of integers int[] anArray; allocates memory for 10 integers anArray = new int[10]; Theandroid-mania.com
  • 15. initialize elements anArray[index] = value; Similarly, you can declare arrays of other types: byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings; Theandroid-mania.com