- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 271 Articles for Java8
627 Views
The private constructor is useful in case we want to restrict the object creation. For example, Singleton pattern can be implemented using a private constructor.ExampleLive Demopublic class Tester { private static Tester instance; private Tester(){} public static Tester getInstance(){ if(instance == null){ instance = new Tester(); } return instance; } public static void main(String[] args) { Tester tester = Tester.getInstance(); Tester tester1 = Tester.getInstance(); System.out.println(tester.equals(tester1)); } }OutputIt will print the output astrue
108 Views
Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −ExampleLive Demopublic class Tester { public static void main(String[] args) { int a = 300; byte b = (byte)a; System.out.println(b); } }OutputIt will print output as −44
1K+ Views
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Method overriding by a subclass is termed as runtime polymorphism. JVM determines the method to be executed at runtime instead of compile time. exampleLive Democlass SuperClass { SuperClass get(){ System.out.println("SuperClass"); return this; } } public class Tester extends SuperClass { Tester get(){ System.out.println("SubClass"); return this; } public static void main(String[] args) { SuperClass tester = new Tester(); tester.get(); } }OutputSubClass
2K+ Views
This article will help you understand what a Marker or Tagged Interface in Java is. Before understanding marker interface, let’s revise on Interface. INTERFACE Similar to an object, an Interface is a blueprint of a class. It consists of static constants and abstract methods. It is a mechanism to achieve abstraction and multiple inheritance in Java. It is declared using the interface keyword. It provides total abstraction, meaning all methods in an interface must be declared with empty body, and all fields must be public, static and final by default. Syntax interface { // constant fields declaration ... Read More
2K+ Views
this can be used to get the current object.this can be used to invoke current object's method.this() can be used to invoke current class constructorthis can be passed as a parameter to a method call.this can be passed as a parameter to a constructor.this can be used to return the current object from the method.
590 Views
Following is the required program.ExampleLive Demopublic class Tester { public static void main(String args[]) { //matrix 1 int a[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 3, 4, 5 } }; //matrix 2 int b[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 1, 2, 4 } }; //result matrix int c[][] = new int[3][3]; // 3 rows and 3 columns // subtract and print matrix for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { c[i][j] = a[i][j] - b[i][j]; System.out.print(c[i][j] + " "); } System.out.println(); } } }Output0 0 0 0 0 0 2 2 1
4K+ Views
ExampleFollowing is the required program.Live Demopublic class Tester { public static int getThirdSmallest(int[] a) { int temp; //sort the array for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } //return third smallest element return a[2]; } public static void main(String args[]) { int a[] = { 11,10,4, 15, 16, 13, 2 }; System.out.println("Third Smallest: " +getThirdSmallest(a)); } }OutputThird smallest: 10
To Continue Learning Please Login
Login with Google