Data types ^J variables and arrays in Java.pptx

sksumayasumaya5 30 views 38 slides Sep 09, 2024
Slide 1
Slide 1 of 38
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38

About This Presentation

Build your presentation Structure Importantly, before you start creating your data charts, you should plan your presentation structure. This will ensure your presentation answers the right questions. Here is a template that we would use at Accenture to create a presentation. You need to download thi...


Slide Content

Sri Venkateswara Engineering College, Suryapet (Affiliated to JNTUH), Approved by AICTE, New Delhi Presented by − Shaik Sumaya (II-II CSE) – 21631A0554 A Small talk on topic Data types, Variables & Arrays in JAVA Under the guidance of Sri. T. Madhu garu, Prof. in CSE Dept.

** Contents ** Data types in java Primitive data types Non-primitive data types Variables in java Rules of variable declaration Types of variables Arrays in java Multidimensional array Applications of arrays

**Data types in Java ** Java programming language has a rich set of data types. The data type is a category of data stored in variables . In java data types are classified into two types 1. primitive data types 2.non primitive data types

“ Primitive data types “ The primitive data types are built-in data types. In java, primitive data types include byte, short, int, long, float, double, char and Boolean.

# Table of primitive data type Data type Meaning Memory size boolean True or false 1 bit byte Two’s complement integer 8 bits char Unicode character 16 bits short Two’s complement integer 16 bits long Two’s complement integer 64 bits float Fractional numbers 32 bits double Fractional numbers 64 bits int Whole numbers 32 bits

* program on primitive data type * public class PrimitiveDataType { public static void main(String[] args) { int a =30; byte b =15; char c = ‘d’; float d = 5.7; System.out.println(“int : ” + a); } }

*Non-primitive data types * Non-primitive data types are also called as “user – defined data types”. All non-primitive data types are implemented using object concepts. Every variable of the non-primitive data type is an object. Ex: string, class, array, etc.

*difference between primitive and non-primitive data types * Primitive data types Non primitive data types It is said to be pre defined data types. It is used for primary applications. Declaration and definition of primitive data types doesn’t require new keyword. It stores in stack memory. Examples: int , float , char , etc. It is said to be as user- defined data types. It is used for secondary applications. Declaration and definition of non-primitive data types require new keyword. It stores in heap memory. Examples: class , interface

*** variables in java *** Definition : A variable is a named memory location used to store a data value. Variables are the named memory locations where we can store different values of the same data type during the program execution.

** Syntax of variables ** Variable declaration syntax : data_type variable_name; (or) data_type variable_name_1, variable_name2,…; Variable initialization syntax : data_type variable_name = value; (or) data_type variable _name_1 = value, variable_name_2 = value,….;

“ Rules of Variable Declaration ” Variable names are case sensitive. Java keywords cannot be used as variable names. Blank spaces cannot be used in variable names. A variable name can consist of capital letters A-Z, lowercase letters a-z digits 0-9, and 2 special characters such as underscore(_) and dollar($) sign.

The first character must not be a digit. There is no limit on the length of a variable name but by convention, it should be between 4 to 15 chars. Examples : myvar myVar MYVAR _myVar $myVar myVar1 myVar_1

* Types of variables * In java variables are classified as follows. Local variables Instance variables or member variables or global variables Static variables or class variables Final variables

Local variables : The variables declared inside a method or a block are known as local variables. A local variable is visible within the method in which it is declared. Instance variables or member variables or global variables : The variables declared inside a class and outside any method, constructor or block are known as instance variables or member variables.

These variables are visible to all the methods of the class. The changes made to these variables by method affects all the methods in the class. These variables are created separate copy for every object of that class. Static variables or class variables : A static variable is a variable that declared using static keyword. The instance variables can be static variables but local variables cannot static variables.

Static variables are initialized only once , at the start of the program execution. The static variable only has one copy per class irrespective of how many objects we create. The static variable is access by using class name. Final variables : A final variable is a variable that declared using final keyword. The final variable is initialized only once and does not allow any method to change it’s value again.

The variable created using final keyword acts as constant. All variables like local, instance and static variables can be final variables.

** Example of Java variables **

Java final variable example class Bike { final int speedlimit =90; void run() { speedlimit = 120; } public static void main(String args []) { Bike b=new Bike(); b.run (); } }

** Arrays in java ** An array is a collection of similar data items stored in contiguous memory locations with single name. In java, arrays are objects and they are created dynamically using new operator. Every array in java is organized using index values. The index value of an array starts with ‘0’ and ends with ‘size-1’.

We use index value to access individual elements of an array. Creating an array : In java, an array must be created using new operator and with a specific size. The size must be an integer value but not a byte, short, or long.

Syntax : data_type array_name[] = new data_type[size]; (or) data_type[] array_name = new data_type[size]; In java, there are two types of arrays and they are as follows. 1) one dimensional array 2) multi dimensional array

Example of an array : public class ArrayExample { public static void main(String[] args) { int list[] = new int[5]; list[0] = 10; System.out.println(“value at index 0” + list[0]); System.out.println(“length of the array “ + list.length); } }

“ Multidimensional Array “ In java, we can create an array with multiple dimensions. We can create 2-dimensional, 3-dimensional, or any dimensional array. In java, multidimensional arrays are arrays of arrays. To create a multidimensional array variable, specify each additional index using another set of square brackets.

* syntax of creation of 2D-Array * data_type array_name[][] = new data_type[rows][columns]; (or) data_type[][] array_name = new data_type[rows][columns];

Example of Two dimensional array

“ Jagged Array in Java”

Example of jagged Array

* Applications of Arrays * Arrays can be used to implement stack and queue data structure. Arrays are good to implement vectors and lists. CPU scheduling algorithm can be implemented using an array. Arrays can be used to implement tree data structure. Matrix operations can be implemented using arrays. The two-dimensional array is used in image processing.

Important Questions Explain about types of data types in Java with examples. 2) Define variable. List out the rules of declaration of variables in Java. 3) Explain about types of variables in java with examples. 4) define Array in java. Explain about different types of arrays in java with example programs. 5)Give applications of arrays in java. 6) Explain about Jagged Array in java with small example program.

Previous Year Question Papers
Tags