UrbanPro
true

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Learn Java Programming with Free Lessons & Tips

Ask a Question

Post a Lesson

All

All

Lessons

Discussion

Lesson Posted on 10/12/2019 Learn Java Programming

What Is Object Oriented Language And Benefits Of OOPS?

Tasneem

Object-Oriented Language:The real entities in this world can be defined as objects. They can be like a tree, fan, tv etc. The language which describes the behaviour of such kind of objects is known as OOPS.It mainly defines two aspects:1. Classes: Class can be defined as a collection of objects having... read more

Object-Oriented Language:
The real entities in this world can be defined as objects. They can be like a tree, fan, tv etc. The language which describes the behaviour of such kind of objects is known as OOPS.
It mainly defines two aspects:
1. Classes: Class can be defined as a collection of objects having the same kind of behaviour.
2. Objects: It is a real-time entity.
The concepts pertaining to the oops are:
1. Inheritance: It is the resemblance of parent class characteristics to the child class.
2. Abstraction: It is only revealing the exact information and hiding unnecessary data.
3. Polymorphism: It is defined as many forms :
a. Overloading.
b. Overriding.
4. Encapsulation: It has the protection to the data that could be given by access specifiers.

read less
Comments
Dislike Bookmark

Lesson Posted on 07/02/2018 Learn Java Programming +5 Java Java Script Training Java Programming Java Programming Core Java

Java Interview Questions & Answers

Dev Group

Dev Group provides an in-depth IT & accounts training in Delhi. All the courses and training classes...

Java Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java Programming Language. As per my experience, good interviewers hardly planned to ask any particular question during your interview,... read more

Java Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java Programming Language. As per my experience, good interviewers hardly planned to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer.

Q 1. What do you know about Java?

Ans. Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Q 2. What are the supported platforms by Java Programming Language?

Ans. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

Ans. Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded.

Ans. It’s compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system.

Ans. Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

Ans. It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Ans. When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.
Q 8. List two Java IDE’s.

Ans. Netbeans, Eclipse, etc.

Q.9. List some Java keywords(unlike C, C++ keywords).

Ans. Some Java keywords are import, super, finally, etc.

Ans. Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Q11. Define class.

Ans. A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

Ans. A class consist of Local variable, instance variables and class variables.

Ans. Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

Ans. Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

read less
Comments
Dislike Bookmark

Lesson Posted on 07/02/2018 Learn Java Programming +4 Java Core Java Java Programming Java Programming

What Is Java? Explain The History Of Java

Dev Group

Dev Group provides an in-depth IT & accounts training in Delhi. All the courses and training classes...

i. Ovierview: Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 ). The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java... read more

i. Ovierview: Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]).

The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.

The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere.

 

ii. Java:

• Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

• Platform Independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.

• Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

• Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.

• Architecture-neutral: Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.

• Portable: Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.

• Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.

• Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.

• Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.

• High Performance: With the use of Just-In-Time compilers, Java enables high performance.

• Distributed: Java is designed for the distributed environment of the internet.

• Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

 

iii. History of Java:

James Gosling initiated Java language project in June 1991 for use in one of his many set-top box projects. The language, initially called ‘Oak’ after an oak tree that stood outside Gosling's office, also went by the name ‘Green’ and ended up later being renamed as Java, from a list of random words.

Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms.

On 13 November, 2006, Sun released much of Java as free and open source software under the terms of the GNU General Public License (GPL).

On 8 May, 2007, Sun finished the process, making all of Java's core code free and open-source, aside from a small portion of code to which Sun did not hold the copyright.

read less
Comments
Dislike Bookmark

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Asked on 27/08/2017 Learn Java Programming

What is serialization in Java?

Answer

Lesson Posted on 29/05/2017 Learn Java Programming +5 Java BTech Tuition BCA Tuition Java Programming Java Certification Classes

Finding a Majority Element

Paras Chawla

Working as a Senior Software Developer(R and D) in the following technologies. 1. Java 2. J2EE/J2ME 3....

Problem Description Task. The goal in this code problem is to check whether an input sequence contains a majority element. Input Format. The first line contains an integer, the next one contains a sequence of non-negative integers 0, 1, . . . ,−1. Constraints. 1 ≤ ?? ≤ 10^5 ; 0 ≤ ????... read more

Problem Description Task.

The goal in this code problem is to check whether an input sequence contains a majority element.

Input Format. The first line contains an integer, the next one contains a sequence of non-negative integers 0, 1, . . . ,−1. Constraints. 1 ≤ ?? ≤ 10^5 ; 0 ≤ ???? ≤ 10^9 for all 0 ≤ ?? < ??.

Output Format. Output 1 if the sequence contains an element that appears strictly more than??/2 times, and 0 otherwise.

Pseudo code

findCandidate(a[], size)

  1. Initialize index and count of majority element

     maj_index = 0, count = 1

  1. Loop for i = 1 to size – 1

    (a) If a[maj_index] == a[i]

          count++

    (b) Else

        count--;

    (c) If count == 0

          maj_index = i;

          count = 1

  1. Return a[maj_index]

 

Java Code

import java.util.Scanner;

 

// Moore - Voting algorithm . Run-time complexity O(n)

public class MajorityElement {

     public static void main(String[] args) {

           Scanner scan = new Scanner(System.in);

           int n = scan.nextInt();

           long array[] = new long[n];

           for (int i = 0; i < n; i++)

                array[i] = scan.nextLong();

           long candidate = findCandidate(array, n);

           System.out.println(candidate);

           int count = 0;

           for (int i = 0; i < n; i++)

                if (array[i] == candidate)

                     count++;

           if (count > (n / 2))

                System.out.println("1");

           else

                System.out.println("0");

     }

 

     static long findCandidate(long array[], int n) {

           int index = 0;

           long count = 1;

           for (int i = 1; i < n; i++) {

                if (array[index] == array[i])

                     count++;

                else

                     count--;

                if (count == 0)

                     {index = i;

                      count = 1;}

           }

           return array[index];

     }

}

 

Output:

10

2 124554847 2 941795895 2 2 2 2 792755190 756617003

 

2

 

1

read less
Comments
Dislike Bookmark

Lesson Posted on 25/05/2017 Learn Java Programming +5 Java Advanced Java coaching Programming in JAVA Java Programming Core Java

How to create a Singleton class?

Gpcomptech

GPCompTech (GP Computer Technologies) is a software training institute located in Chandanagar Hyderabad,...

How to create a Singleton class: Q) What is a singleton class? A) In simple words, a singleton class is a class which can have only one instance at any point of time throughout the application and provides a global point of access to its instance. Q) How can you create a singleton class? A) class... read more

How to create a Singleton class:

Q) What is a singleton class?

A) In simple words, a singleton class is a class which can have only one instance at any point of time throughout the application and provides a global point of access to its instance.

 

Q) How can you create a singleton class?

A)

class Singleton {

       public static final Singleton INSTANCE = new Singleton();

       private Singleton() {

       }

}

                 This is a simple example for a singleton class where I used a public static final INSTANCE variable and initialized it with a Singleton object. Since INSTANCE is public static final, it can’t be reassigned and it is thread safe as well.

                 Singleton’s constructor is made private to prevent the instantiation of Singleton class from other classes. That means no other class can create an object for Singleton class with the new() operator and also no other class can extend this Singleton class with extends keyword. If any class wants to have an instance of Singleton it can have by accessing INSTANCE field of Singleton class like Singelton. INSTANCE as its static.

 

  1. Q) So here, Singleton instance will be created at the time of class loading that means when the Singleton class is loaded by JVM into memory. So it’s an early loading/instantiation of Singleton class and it occupies memory even when its not required yet, so what If we want this singleton instance to be created when it’s really required?

 

  1. A) I understand that you are interested in lazy loading/instantiation of Singleton. Here it is

 

class Singleton {

       private static Singleton INSTANCE;

       private Singleton() {

       }

       public static synchronized Singleton getInstance() {

              if (INSTANCE == null) {

                     INSTANCE = new Singleton();

              }

              return INSTANCE;

       }

}

 

  1. Q) You synchronized the whole method

       public static synchronized Singleton getInstance()

Don’t you see any performance overhead with this approach?

Only one thread can access the method getInstance at a time to get the Singleton instance and rest of all other threads need to wait even though INSTANCE is not null as its already created.

A) Let me create it using synchronized block instead of synchronized method, here how it goes

 

class Singleton {

       private static Singleton INSTANCE;

       private Singleton() {

       }

       public static Singleton getInstance() {

              if (INSTANCE == null) {             //----- 1st

                     synchronized (Singleton.class) {

                           if (INSTANCE == null) {   //----- 2nd

                                  INSTANCE = new Singleton();

                           }

                     }

              }

              return INSTANCE;

       }

}

 

  1. Q) Why do you use null check twice here?
  2. A) I think I don’t need to explain you why did I use first null check, as you know I am going to create Singleton instance in case INSTANCE is null otherwise I ll return the already created instance.

2nd null check is being used to ensure thread safety.

For suppose, 2 threads are concurrently trying to get the access for getInstance() and two threads are gone through the 1st null check and trying to enter into synchronized block, but only one thread(assume thread-1) got the lock on Singleton.class’s  object and enters the synchronized block while the other thread (assume thread-2)waiting for the lock. Once thread-1 went through 2nd null check then it creates a Singleton instance as INSTANCE is null and leaves the synchronized block and thus releases lock. Now, the waiting thread ( thread-2) holds the lock and enters the synchronized block.

 

If there is no 2nd null check there then there is a chance for the thread-2 to create another instance of Singleton class and thus it breaks the singleton here. For that reason we have 2nd null check here. So the thread-2 sees that INSTANCE is not null and comes out from the synchronized block without creating Singleton instance.

 

  1. Q) Ok. So double checking is for thread safety of a singleton.

 

 

  1. A) yes

 

Q) Do you think that, this Singleton class is still singleton?

I can break your singleton with the help of reflection like below

 

public class Test {

       public static void main(String args[]) {

              Singleton singleton1 = Singleton.getInstance();

              Singleton singleton2 = null;

              try {

                     // Test is package name contains Singleton class

                     Class singletonClass = Class.forName("Test.Singleton");

                     // as we know there is only one private constructor

                     Constructor cons = singletonClass.getDeclaredConstructor();

                     cons.setAccessible(true);

                     singleton2 = (Singleton) cons.newInstance();

                     System.out.println(singleton1 == singleton2 ? "Objects are equal" : "Objects are different");

              } catch (ClassNotFoundException | InstantiationException

                           | IllegalAccessException | IllegalArgumentException

                           | InvocationTargetException | NoSuchMethodException

                            | SecurityException e) {

                     e.printStackTrace();

              }

       }

}

 

  1. A) In that case I can implement Singleton like below for not to be broken with reflection

class Singleton {

       private static Singleton INSTANCE;

       private Singleton() {

              if (INSTANCE != null)

                     throw new RuntimeException(

                                  "Singleton can not be created more than once.");

       }

       public static Singleton getInstance() {

              if (INSTANCE == null) {

                     synchronized (Singleton.class) {

                           if (INSTANCE == null) {

                                  INSTANCE = new Singleton();

                           }

                     }

              }

              return INSTANCE;

       }

}

 

Private constructor will be called to instantiate the Singleton object through reflection. So we can throw some exception If INSTANCE field is not null (that means its already created).

 

  1. Q) So you think this implementation saves your Singleton from being broken from reflection. In my previous program, I am trying to create a Singleton object (singleton1) with the help of getInstance() and after that trying to create another object (singleton2) with the help of reflection. What happens If I do this in reverse like below

 

public class Test {

       public static void main(String args[]) {

//            Singleton singleton1 = Singleton.getInstance();

              Singleton singleton2 = null;

              try {

                     // Test is package name contains Singleton class

                     Class singletonClass = Class.forName("Test.Singleton");

                     // as we know there is only one private constructor

                     Constructor cons = singletonClass.getDeclaredConstructor();

                     cons.setAccessible(true);

                     singleton2 = (Singleton) cons.newInstance();

                     Singleton singleton1 = Singleton.getInstance();

                     System.out.println(singleton1 == singleton2 ? "Objects are equal" : "Objects are different");

              } catch (ClassNotFoundException | InstantiationException

                            | IllegalAccessException | IllegalArgumentException

                           | InvocationTargetException | NoSuchMethodException

                           | SecurityException e) {

                     e.printStackTrace();

              }

       }

}

 

Output:

 Objects are different

 

Here, If I try to create a Singleton object (singleton2) with the help of reflection first then it can proceed to create an Singleton object as the static field INSTANCE is still null at that time as getInstance() is not yet invoked. So we are allowed to create an many as objects we want for the Singleton using reflection before getInstance() is invoked and thus breaks the singleton. What do you say?

 

  1. A) Yeah, you are right. Let me come to you with slightly modified implementation of Singleton which protects itself from being broken with reflection.

 

class Singleton {

       private static Singleton INSTANCE;

       private static boolean instanceCreated = false;

       private Singleton() {

          & read less

Comments
Dislike Bookmark

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Lesson Posted on 13/05/2017 Learn Java Programming +6 Programming in JAVA BTech Tuition Engineering Diploma Tuition Java Programming Java Core Java

Java Overview

Akshay

Java platform overview Java platform components The Java platform consists of the following components. The Java language Like any programming language, the Java language has its own structure, syntax rules, and programming paradigm. The Java language's programming paradigm is based on the concept... read more

Java platform overview

Java platform components

The Java platform consists of the following components.

The Java language

Like any programming language, the Java language has its own structure, syntax rules, and programming paradigm. The Java language's programming paradigm is based on the concept of object-oriented programming (OOP), which the language's features support.

The Java language is a C-language derivative, so its syntax rules look much like C's. For example, code blocks are modularized into methods and delimited by braces ({ and }), and variables are declared before they are used.

Structurally, the Java language starts with packages. A package is the Java language's namespace mechanism. Within packages are classes, and within classes are methods, variables, constants, and more.

read less
Comments
Dislike Bookmark

Answered on 31/03/2017 Learn Java Programming +1 BTech Tuition

Chetana t.

Tutor

learn basic java programme will take around 1-2 month and fee around 2K-3K. better go with basic+advanced java , it will worth by learning as well as fee wise.
Answers 66 Comments
Dislike Bookmark

Answered on 18/02/2017 Learn Java Programming +1 BTech Tuition

Prashanth Kannadaguli

BEST Technical Trainer & Freelancer

Attend online classes, use video lectures etc.
Answers 45 Comments
Dislike Bookmark

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Answered on 20/01/2017 Learn Java Programming +1 BTech Tuition

Srinivas Nangana

Java and dev ops engineer(Core Java,JDBC,Servlet, SQL, Spring, Spring MVC, Spring JDBC) Hyd

1.String is immutable. 2.String is class in java also called referenced data type. 3.It holds collection of characters with out having size limitation. 4.String class contains no of methods to do operations on strings like trim(), charAt(), to Upper Case() etc. 5.String only contains string pool area. 6.String... read more
1.String is immutable. 2.String is class in java also called referenced data type. 3.It holds collection of characters with out having size limitation. 4.String class contains no of methods to do operations on strings like trim(), charAt(), to Upper Case() etc. 5.String only contains string pool area. 6.String objects which are there in string constant pool area not eligible for garbage collector. read less
Answers 36 Comments
Dislike Bookmark

About UrbanPro

UrbanPro.com helps you to connect with the best BTech Tuition in India. Post Your Requirement today and get connected.

Overview

Lessons 18

Total Shares  

+ Follow 114,399 Followers

You can also Learn

Top Contributors

Connect with Expert Tutors & Institutes for Java Programming

x

Ask a Question

Please enter your Question

Please select a Tag

X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more