Take BTech Tuition from the Best Tutors
Search in
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 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.
Lesson Posted on 07/02/2018 Learn Java Programming
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, 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. Netbeans, Eclipse, etc.
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.
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.
Lesson Posted on 07/02/2018 Learn 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 [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 lessTake BTech Tuition from the Best Tutors
Asked on 27/08/2017 Learn Java Programming
Lesson Posted on 29/05/2017 Learn Java Programming
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 ≤ ???? ≤ 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)
maj_index = 0, count = 1
(a) If a[maj_index] == a[i]
count++
(b) Else
count--;
(c) If count == 0
maj_index = i;
count = 1
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 lessLesson Posted on 25/05/2017 Learn Java Programming
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 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.
class Singleton {
private static Singleton INSTANCE;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
}
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;
}
}
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.
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();
}
}
}
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).
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?
class Singleton {
private static Singleton INSTANCE;
private static boolean instanceCreated = false;
private Singleton() {
& read less
Take BTech Tuition from the Best Tutors
Lesson Posted on 13/05/2017 Learn Java Programming
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 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 lessAnswered on 31/03/2017 Learn Java Programming
Chetana t.
Tutor
Answered on 18/02/2017 Learn Java Programming
Prashanth Kannadaguli
BEST Technical Trainer & Freelancer
Take BTech Tuition from the Best Tutors
Answered on 20/01/2017 Learn Java Programming
Srinivas Nangana
Java and dev ops engineer(Core Java,JDBC,Servlet, SQL, Spring, Spring MVC, Spring JDBC) Hyd
UrbanPro.com helps you to connect with the best BTech Tuition in India. Post Your Requirement today and get connected.
Ask a Question
The best tutors for BTech Tuition Classes are on UrbanPro
The best Tutors for BTech Tuition Classes are on UrbanPro