Sie sind auf Seite 1von 14

Data types

Before using a variable, we should specify what type of variable it is.

When we specify the datatype, the system can understand the memory requirements
and the operations allowed on the corresponding variables.

Ex: to store age of a person, then we specify the variable as an integer.


to store temperature of a city, then we define the it as double.

An integer occupies 4 bytes of memory where as a double occupy 8 bytes of


memory.

Integer data types are:

 Byte 1 byte -128 to 127


 short 2 bytes -32,768 to 32,767
 int 4 bytes -2,147,483,648 to 2,147,483,647
 long 8 bytes
 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80

datatype variable = initial value;

ex : int a = 100;

Data Type
 Size: 32 bit i.e n = 32 0 to 31
Value: -232 to 232 – 1 -2n to 2n -1
8 bits - 28 to 28-1 -128 to 127
16 bits -216 to 216-1 -32,768 to 32,767
32 bits -232 to 232-1 -2,147,483,648 to 2,147,483,647

64 bits -264 to 264-1


-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808

Java actually uses Unicode, which includes ASCII and other characters from
languages around the world.

ASCII Table has 127 chrs.

Byte 8-bit signed two's complement integer


save space in large arrays, mainly in place of integers, since a byte is
four times smaller than an integer.

Byte - 1 char – 8 bits of info

short a 16-bit signed two's complement integer

int a 32-bit signed two's complement integer.


int – 4 bytes 4x 8 bits = 32 bits

long Long data type is a 64-bit signed two's complement integer

float to save memory in large arrays of floating point numbers


double

Boolean represents one bit of information

char a single 16-bit Unicode character


BYTE

As it occupies 1 byte of memory, we can store a value between -128 to 127.

If we try to store a value bigger than that we will get a compilation error.

byte b=25; is valid


Byte c=225; is not valid ✕

To work with single characters (ASCII values up to 127), we can use byte type as it uses only 1
byte of memory (against char which takes 2 bytes of memory).

SHORT

To store integers in the range -32768 to 32767.


Any value out of this range cannot be kept as short.
INT

Default integer type.


Most of the times we use int type only to work
We can use up to a 10 digit number (up to 200 crores nearly) with int type.

LONG

To store a value bigger than int range, we should use long type.

With long, we can store up to a 19 digit number.

When using a constant bigger than int range, we should suffix it with ‘l’ or ‘L’ to indicate it to be
a long value.

long c=123456789012345L;

CHAR

char type takes 2 bytes of memory to support UniCode characters

As per UniCode, we have 65536 (216) characters numbered from 0 to 65535.

The char type cannot take negative values.

Numeric type can take both positive and negative values.

The first 256 (numbered from 0 to 255) characters of UniCode are the ASCII set of characters
only.

So UniCode is compatible with ASCII.


So both in ASCII and UniCode ‘A’ is 65, ‘B’ is 66, ‘C’ is 67, ‘ a’ is 97, ‘b’ is 98, ‘0’ is 48, ‘1’ is
49, ‘\n’ is 10, ‘\t’ is 9.
Eg: char a=55; is valid

FLOAT

numbers with a fractional part, we can use float type

 float b=3.6f
 float c=(float)3.6
DOUBLE

This is the default datatype to store decimal (real numbers) values

double a=3.9;
double b=834;
double c=’A’;

Boolean

to store logical values

boolean a=true

boolean g=10<20;

boolean d=false

Package in Java is a mechanism to encapsulate a group of classes, sub packages and


interfaces.

Packages are used for:


 Preventing naming conflicts.
Example:
There can be two classes by the name Employee
in two packages,
1) cse.Employee and
2) ee.Employee

CSE IS A PACKAGE.
EMPLOYEE IS A CLASS
class employee
{

College – technical—department – CSE---EMPLOYEE


College – technical—department – EE- EMPLOYEE

CSE
EE

 Providing controlled access:


 Protected and default have package level access control.
 A protected member is accessible by classes in the same package and its
subclasses.
 A default member (without any access specifier) is accessible by classes
in the same package only.
 Packages can be considered as data encapsulation (or data-
hiding).
 Put related classes into packages.
 After that we can simply write
import a class from existing packages and
use it in our program.
 A packages is a container for a group of related classes
 We can reuse existing classes
from the Java packages
written by vendors
as many times as we need it in our program.

Adding a class to a Package


We can add more classes to a

 Created package

i.e. by using package name at the top of the program and saving it in the package directory.

Naming Conventions

Packages are named in reverse order.

Ex: in a college, the recommended convention is

 college.tech.cse.Employee
 college.tech.ee.Employee
Packages are
Built-in Packages

User-defined packages

Built-in Packages consist of a large number of classes which are a part of Java API.

Commonly used built-in packages are

java.lang

java.io

java.util

java.applet

java.awt ---

java.net

import java.applet.addApplet.class3; // number of classes n


which you have created

import java.applet.addApplet.*;

import java.lang
java.lang Contains language support classes (e.g classed which defines primitive data types,
math operations). This package is automatically imported.

java.io Contains classed for supporting input / output operations.


import java.io // all print functions defined in a package;

println(“Welcome to Java” );

println(“Iam from India”);

println(“Hello World!”);

println( ) // its in a class // class is in java.io package

java.util Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.

java.applet Contains classes for creating Applets.

java.awt Abstract window toolkit

Contain classes for implementing the components for graphical user interfaces (like button ,
menus ).

java.net Contain classes for supporting networking operations.


User-defined packages
These are the packages that are defined by the user.

First we create a directory by the name myPackage (name should be same as the name of the
package).

Then create the MyClass inside the directory with the first statement being the package name.
package mypackage ;

public class myclass // data members + functions

public void getNames(String s) ///String is a class now

//s is an object for the String class

System.out.println(s);

}
Note :

Name of the package must be same as the directory under which this file is
saved

Now we can use the MyClass class in our program

import myPackage.MyClass;

import java.io;

// import MyClass class from names package

public class printName

public static void main(String args[ ] )


{

String name = “My Name is B.M.Rao”; // initializing string name

String name = “My Name is Jaya”; // initializing string name

String name = “My Name is Nagamani”; // initializing string name

String name = “My Name is Eswari”;

// create instance of class we have created in the package

MyClass object = new MyClass( );

Object.getNames (name);

System.out.println(“welcome”)

Note :

MyClass.java must be saved inside the myPackage directory since it is a part of


the package.

//import java.io.myPackage.MyClass;
import java.io;
import myPackage.*;

c:> user/b/jdk/java/bin/ bin is a directory

Subpackages

Packages that are inside another package are the subpackages.

These are not imported by default, they have to be imported explicitly

Ex: import java.util.* ;

util is a subpackage created inside java package.

 import java.util.vector ;

// to import Vector class from util package which is contained


inside java.

Class vector
{
Public static void main(string args[ ]

}
import java.util.*;

// imports all the classes from util package.

Variables are reserved memory locations to store values.


i.e. when you create a variable you reserve some space in the memory.

Based on the data type of a variable, the operating system allocates memory and decides what
can be stored in the reserved memory.

By assigning different data types to variables, you can store integers, decimals, or characters in
these variables.

There are two data types available in Java −

 Primitive Data Types - eight primitive data types supported by Java


 Reference/Object Data Types

Das könnte Ihnen auch gefallen