Java Questions with Answers #2

What are the different data types in JAVA? Explain each of them with example.

Various data types used in java are byte, short, long, char, float, double, int, Boolean

byte :- This is in fact smallest integer type of data types. Its width is of 8-bits with the range -128 to 127.Syntax to declare variable as byte is as 
     byte i,j;

short:- This data type is also used for defining the signed numerical variables with a width of 16-bits and having a range from -32768 to 32767. Syntax to declare variable as short is as 
     short a,b;

int:- This is the most commonly used data type for defining the numerical data.
The width of this data type is 32-bit having a range of -2,147,483,648 to 2,417,483,647. Syntax to declare variable as int is as
     int I,j;

long:- Sometimes when int is not sufficient for declaring some data then long is used. The range of long is really very long and it is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The declaration can be 
     long x,y;

float:- To represent the real number float data type can be used. The width is 32- bit and range of this data type is 1.4e-045 to 3.4e+038. Syntax to declare variable as float is as float p,q; double:- To represent the real number of large range the double data type is used. Its width is 64-bit having the range 4.9e-324 to 1.8e+308. Syntax to declare variable as byte is as 
      double a,b;

char:- This data type is used to represent the character type of data. The width of this data type is 16-bit and its range is 0 to 65,536. Syntax to declare variable as byte 
     char a;

Boolean:- Boolean is a simple data type which denotes a value to be either true or false.

What do you mean by Dynamic Initialization?

Java is a flexible programming language which allows the dynamic initialization of variables. In other words, at the time of declaration one can initialize the variables. In java we can declare the variable at any place before it is used.
     Ex.:- int a=10;
          float d=2.34f;

What do you mean by Variable? What are the rules for variable declaration?

Variable is a fundamental unit of storage in java. The variables are used in combination with identifiers, data types, operators and some value for initialization. The syntax of variable declaration will be:
data_type name_of_variable[=initialization];

Following are the rules for variable declaration:-
1. The variable name should not start with digits.
2. No special character is allowed in identifier except underscore.
3. There should not be any blank space with the identifier name.
4. The identifier name should not be a keyword.
5. The identifier name should be meaningful.

Which are of the following variable name is valid? 
Variable Valid/Invalid  Reason
John Valid -------
T_raise  Valid -------
(area)  Invalid ( and ) not allowed
char  Invalid It is keyword
25th  Invalid Digits not allowed
Average  Valid -------
distance  Valid -------
signed  Invalid It is keyword
define  Invalid It is keyword
_hello  valid -------

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.