Data types in java with example programs

  • The data type is something which gives information about
  • Size of the memory location and range of the data that can be accommodated inside that location.
  • Possible legal operations those can be performed on that location.
  • What type of result come out from an expression when we use these types inside that expression.
  • Whichever the keyword gives these semantics is treated as "data type".
  • The java language provides different categories of data types. 

1.Primitives Data types:

  • The primitive data types are the predefined data types given by the programming language and they are meant for storing a single value.
  • Based on the type and range of data, primitive types are divided into 8 types.

1.Integer category: 

  • This category can be used for storing numbers which can be either positive value  or negative value without decimal points.
  • In this category we have 4 primitive data types whose memory sizes are different. 
  • All the 4 types under this category are used for storing same data. But there ranges are different. The java language is providing 4 types under Integer category. So that the memory is utilized efficiently. 
  1. byte  
  2. short
  3. int
  4. long 
data types in java


byte data type in java:

  • Size 1 byte. i.e 8 bits
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (2^7 -1)
  • Default value is 0
Java example program on byte data type:

  1. package com.instanceofjava;
  2. Class ByteDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  byte x=10;
  7.  byte y=20;
  8.  
  9.   byte z= (byte) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30 

 short data type in java:

  • Size 2 bytes.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (2^15-1)
  • Default value is 0

Java example program on short data type:

  1. package com.instanceofjava;
  2. Class ShortDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  short x=10;
  7.  short y=20;
  8.  
  9.   short z= (short) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

int data type in java:

  • Size 4 bytes.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647 (2^31 -1)
  • Default value is 0

Java example program on int data type:

  1. package com.instanceofjava;
  2. Class IntDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9.   int z=x+y;
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

long data type in java:
  • Size 8 bytes.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
  • Default value is 0

Java example program on long data type:

  1. package com.instanceofjava;
  2. Class LongDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  long x=109876677777l;
  7.  long y=20666677766l;
  8.  
  9.  long z=x+y;
  10.  
  11.  System.out.println(z);
  12.  
  13. }

  14. }



Output:

  1. 130543355543

2.Floating point category: 

  • This category can be used for storing numbers which can be either +VE or –VE with decimal point. In the floating point category we have two types whose size is different. The two data types are float and double.
  • Both the data types under the floating point category are used for storing same data but there range is different. The java language provide as two data types under floating point category so that memory is utilized efficiently. 
  • Default value of float is 0
  • Default value of double is 0.0 
  1. float
  2. double


float data type in java

float data type in java:

  • Size 4 bytes.
  • Minimum value is 1.4e-45
  • Maximum value is 3.4e38
  • Default value is 0.0f

Java example program on float data type:

  1. package com.instanceofjava;
  2. Class FloatDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  float x=231231.0031f;
  7.  float y= 53423423434.43231f;

  8.  float z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.3423653E10

double data type in java:
  • Size 8 bytes.
  • Minimum value is 4.9e-324
  • Maximum value is 1.8e308
  • Default value is 0.0d

Java example program on double data type:

  1. package com.instanceofjava;
  2. Class DoubleDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  double x=231231.0031f;
  7.  double y= 53423423434.43231f;

  8.  double z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.342365466543541E10

3.Character category: 

  • This category can be used for storing a single character. A character can be represented by alphabets, a digit and special symbols. 
  • This category contains only one data type an it is char.
  • Default value of char is "one space"
  1. char

char data type in java:
  • Size 2  bytes.
  • Minimum value is 0
  • Maximum value is 65,535
  • Default value is ' '

Java example program on char data type:

  1. package com.instanceofjava;
  2. Class CharDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  char a='a';
  7.  System.out.println(a);
  8.  
  9. }

  10. }


Output:

  1. a

4.Boolean category: 

  • This category can be used for storing either true or false. Under the Boolean category only one data type an it is Boolean. The size is dependent on JVM to JVM.

boolean data type in java:

  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false. '

Java example program on boolean data type:

  1. package com.instanceofjava;
  2. Class BooleanDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. boolean x = false;
  7. System.out.println(x);
  8.  
  9. }

  10. }

Output:

  1. false

Top 10 interview questions on java interfaces

1.What is an interface in Java.

  • Before Java 8 interfaces are pure abstract classes which allow us to define public static final variables and public abstract methods(by default).
  • In java 8 introduced static methods and default methods in interfaces. 
  • We can develop interfaces by using "interface" keyword.  
  • A class will implements all the methods in an interface.



  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x=12;
  5. void show(); 

  6. }

  1. package com.instanceofjava;
  2. Class A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }

  7. }



  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. int x=12;
  5. void show(); 

  6.   
  7. default void display(){
  8.  
  9. System.out.println("default method of interface");
  10.  
  11. }
  12.  
  13. Static void print(String str){
  14.  
  15. System.out.println("Static method of interface:"+str);
  16.  
  17. }
  18.  
  19.  
  20. }




2.What will happen if we define a concrete method in an interface?
  • By default interface methods are abstract.
  • if we declare any concrete method in an interface compile time error will come.
  • Error:Abstract methods do not specify a body

Interface concrete method in java


3.Can we create non static variables in an interface?

  • No.We can not create non static variables in an interface.
  • If we try to create non static variables compile time error comes.
  • By default members will be treated as public static final variables so it expects some value to be initialized.

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error


  5. }


4.What will happen if we not initialize variables in an interface.
  • Compile time error will come because by default members will be treated as public static final variables so it expects some value to be initialized.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error: The blank final field y may not have been initialized


  5. }
5.Can we declare interface members as private or protected?
  • No.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
  5. public, static & final are permitted
  6. protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
  7. public, static & final are permitted

  8. }


7.When we need to use extends and implements?
  • A class will implements an interface.
  • A class will extends another class.
  • An interface extends another interface.

6.Can we create object for an interface?

  • NO. We can not create object for interface.
  • We can create a variable fro an interface

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }
  7. public static void main(String args[]){
  8.  
  9.  JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
  10.  
  11. }
  12. }

7.Can we declare interface as final?

  • No. Compile time error will come.
  • Error: Illegal modifier for the interface Sample; only public & abstract are permitted
declare interface final in java



8.Can we declare constructor  inside an interface?

  • No. Interfaces does not allow constructors.
  • The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.

final interface interview freshers


9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?

  • A class which implements an interface should implement all the methods (abstract) otherwise compiler will throw an error.
  • The type Example must implement the inherited abstract method JavaInterface.show() 
  • If we declare class as abstract no need to implement methods. 
  • No need of overriding default and static methods.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  • }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface { // The type Example must implement the inherited
  3. abstract method JavaInterface.show()
  4.  
  5. public static void main(String args[]){
  6.  

  7. }
  8. }

10.How can we access same variables defined in two interfaces implemented by a class?

  • By Using corresponding interface.variable_name we can access variables of corresponding interfaces.
11.If  Same method is defined in two interfaces can we override this method in class implementing these interfaces.
  • Yes implementing the method once is enough in class.
  • A class cannot implement two interfaces that have methods with same name but different return type.

Hibernate configuration file

1.hibernate.dialect:
  • This property hibernate.dialect makes Hibernate generate the appropriate SQL for the given database.
2.hibernate.connection.driver_class:
  • This property hibernate.connection.driver_class specifies jdbc driver class name 
3.hibernate.connection.username:
  • This property hibernate.connection.username specifies database user name.
4.hibernate.connection.password:
  • This property hibernate.connection.password specifies password.



hibernate.cfg.xml file structure


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.  
  7. <session-factory>
  8.  
  9. <!-- Related to the connection START -->
  10. <property name="connection.driver_class">Driver Class Name </property>
  11. <property name="connection.url">URL </property>
  12. <property name="connection.user">user name</property>
  13. <property name="connection.password">password</property>
  14. <!-- Related to the connection END -->

  15. <!-- Related to hibernate properties START -->
  16. <property name="show_sql">true/false</property>
  17. <property name="dialet">Database dialet class</property>
  18. <property name="hbm2ddl.auto">create/update or what ever</property>
  19. <property name="hibernate.jdbc.batch_size">hibernate container that every N rows to be
  20. inserted as batch.</property> 
  21. <!-- Related to hibernate properties END-->
  22.  
  23. <!-- Related to mapping START-->
  24. <mapping resource="hbm file 1 name .xml" />
  25. <mapping resource="hbm file 2 name .xml" />
  26. <!-- Related to the mapping END -->
  27.  
  28. </session-factory>
  29.  
  30. </hibernate-configuration>




Example hibernate.cfg.xml. file  for mysql


hibernate configuration file java

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url"> jdbc:mysql://localhost/test</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.MySQLDialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>


Example hibernate.cfg.xml. file for oracle Db

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  9. <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.Oracle9Dialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>



Constructor in interface ?

Can we write constructor inside interface in java?

  • No. Interfaces does not allow constructors.
  • Why interface does not have constructor? The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.
  • what happens when a constructor is defined for an interface



What will happens if we try to create constructor inside interfaces in java

  • If we try to create constructor in interface compile time error will come.
  • Error description: Interfaces cannot have constructors.


  1. public interface sample{
  2.  
  3.  int a=10;

  4. sample(){//Interfaces cannot have constructors.
  5.  
  6. }




constructor in interface java





Interfaces in Java 8:

  • Before java 8 interfaces allows only public abstract methods.
  • If we declare any method in interface with default it will be treated as public abstract method.
  • Interface methods doesn't have body. The class which implements interfaces are responsible for implementing unimplemented methods of interface.
  • But in Java 8 static and default methods added.

Default methods:

  • Defaults methods are also  known as defender methods or virtual extension methods
  • Default methods will help us to avoid utility classes.
  • We can define utility methods inside the interface and use it in all classes which is implementing.
  • One of the major reason to introduce this default methods in java 8 is to support lambda expressions in collections API and to enhance.


  1. package com.instanceofjava;
  2. interface Java8InterfaceDemo{
  3.  
  4. abstract void print();
  5.   
  6. default void display(){
  7.  
  8. System.out.println("default method of interface");
  9.  
  10. }
  11.  
  12. }

  1. package com.instanceofjava;
  2. class Sample implements Java8InterfaceDemo{
  3.  
  4. void print(){
  5. System.out.print("overridden method ")
  6.  }
  7. public static void main(String[] args){
  8.   
  9. Sample obj= new Sample();
  10.  
  11. obj.print(); // calling implemented method
  12. obj.display(); // calling inherited method
  13.  
  14. }
  15.  
  16. }

Output:

  1. overridden method
  2. default method of interface
  3. default method of interface

Static methods in Java 8:

  • These static method will act as helper methods.
  • These methods are the parts of interface not belongs to implementation class objects.

  1. package com.interfacesinJava8;
  2. interface StaticInterface{
  3.  
  4. Static void print(String str){
  5.  
  6. System.out.println("Static method of interface:"+str);
  7.  
  8. }
  9. }

  1. package com.instanceofjava;
  2. class Demo implements StaticInterface{
  3.  
  4. public static void main(String[] args){
  5.   
  6.  StaticInterface.print("Java 8")
  7.  
  8. }
  9.  
  10. }

Output:

  1. Static method of interface: Java 8

Can we create private constructor in java

1.Can a constructor in Java be private?
  • Yes we can declare private constructor in java.
  • If we declare constructor as private we can not able to create object of the class.
  • In singleton design pattern we use this private constructor. 

private constructor in java example program


 2.In what scenarios we will use private constructor in java.

  •  Singleton Design pattern
  • It wont allow class to be sub classed.
  • It wont allow to create object outside the class.
  • If All Constant methods is there in our class we can use private constructor.
  • If all methods are static then we can use private constructor.



 3.What will happen if we extends a class which is having private constructor.

  •  If we try to extend a class which is having private constructor compile time error will come.
  • Implicit super constructor is not visible for default constructor. Must define an explicit constructor

private constructor in java


Singleton Design pattern:


  1. package com.privateConstructorSingleTon;
  2.  
  3. public class SingletonClass {

  4. private static SingletonClass object;
  5.  
  6. private SingletonClass ()
  7. {
  8.         System.out.println("Singleton(): Private constructor invoked");
  9. }
  10.  
  11. public static SingletonClass getInstance()
  12. {
  13.  
  14. if (object == null)
  15. {
  16.  
  17. System.out.println("getInstance(): First time getInstance was called and object created !");
  18. object = new SingletonClass ();
  19.  
  20.  }
  21.  
  22. return object;
  23.  
  24. }
  25.  
  26. }
     



  1. package instanceofjava;
  2.  
  3. public class SingletonObjectDemo {

  4. public static void main(String args[]) {
  5.  
  6.      SingletonClass s1= SingletonClass .getInstance();
  7.      SingletonClass s2= SingletonClass .getInstance();
  8.      System.out.println(s1.hashCode());
  9.      System.out.println(s2.hashCode());
  10.  
  11. }
  12. }

Output:

  1. getInstance(): First time getInstance was called and object created !
  2. Singleton(): Private constructor invoked
  3. 655022016
  4. 655022016

Java Static Constructor : is it possible to create static constructor?

1.Is it possible to create static constructor in java?
  • No. We can not create constructor with static.
  • If we try to create a static constructor compile time error will come: Illegal modifier for the constructor
Static construcotr in java interview question



2.What is the real use of constructor in java? 

  • Constructors will be used to assign instance variables with default values.
  • Whenever object is created constructor will be called so the default values for the instance variables will be assigned in this constructor.
  • Top 15 Java Interview Questions on Constructors

  1. public class ConstructorDemo {
  2.  
  3.  int a,b;

  4. ConstructorDemo (int x, int y){
  5.  
  6. a=x;
  7. b=y;
  8.  
  9. }
  10. public static void main(String[] args) {
  11.  
  12.         ConstructorDemo ob= new ConstructorDemo ();
  13.  
  14.     }
  15. }


3. What is the use of static in java?
  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.
4.Is there any alternative solution for static constructor in java

  • Static means class level.
  • Constructor will be use to assign initial values for instance variables
  • static and constructor are different and opposite from each other.
  • To assign initial values for instance variable we use constructor.
  • To assign static variables we use Static Blocks
  • We can use static blocks to initialize static variables in java.


5.what is static block in java?

  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }
6. write a java program to assign static variables using static block


  1. package com.staticInitializer;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

  1.Static Variables 

  2.Static Methods 

  3.Static Blocks

  4.Main method

Select Menu