Object Oriented Programming Articles

Page 199 of 589

How is down-casting possible in Java?

Syed Javed
Syed Javed
Updated on 11-Mar-2026 229 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 −Examplepublic 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

Read More

How to import java.lang.String class in Java?

Fendadis John
Fendadis John
Updated on 11-Mar-2026 18K+ Views

To import any package in the current class you need to use the import keyword asimport packagename;Exampleimport java.lang.String; public class Sample {    public static void main(String args[]) {       String s = new String("Hello");       System.out.println(s);    } }OutputHello

Read More

String Concatenation by concat() method.

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 278 Views

You can concatenate two strings using the concat() method of the String class. This class concatenates the specified string to the end of this string.Examplepublic class Test {    public static void main(String args[]){       String str1 = "Krishna";       String str2 = "Kasyap";       System.out.println(str1.concat(str2));    } }OutputkrishnaKasyap

Read More

Local variables in Java

Syed Javed
Syed Javed
Updated on 11-Mar-2026 2K+ Views

Local variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, or block.Local variables are implemented at stack level internally.There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.ExampleHere, age is a local variable. This is defined inside pupAge()method and its scope is limited to only ...

Read More

Member variables in Java

Kumar Varma
Kumar Varma
Updated on 11-Mar-2026 11K+ Views

Member variables are known as instance variables in java.Instance variables are declared in a class, but outside a method, constructor or any block.When space is allocated for an object in the heap, a slot for each instance variable value is created.Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.Instance variables can be declared in a class level ...

Read More

Instance variables in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 24K+ Views

Instance variables are declared in a class, but outside a method, constructor or any block.When space is allocated for an object in the heap, a slot for each instance variable value is created.Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.Instance variables can be declared at the class level before or after use.Access modifiers can be given ...

Read More

Find the 3rd smallest number in a Java array.

Kumar Varma
Kumar Varma
Updated on 11-Mar-2026 5K+ Views

ExampleFollowing is the required program.public 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

Read More

A closer look at Java "Hello World" program

Daniol Thomas
Daniol Thomas
Updated on 11-Mar-2026 397 Views

Let us look at a simple code that will print the words Hello World.Examplepublic class MyFirstJavaProgram {    /* This is my first java program. *    This will print 'Hello World' as the output */    public static void main(String []args) {       System.out.println("Hello World"); // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to the directory where you saved the class. Assume it's ...

Read More

Why can't we define a static method in a Java interface?

Anjana
Anjana
Updated on 11-Mar-2026 832 Views

From Java 8 onwards, static methods are allowed in Java interfaces.An interface can also have static helper methods from Java 8 onwards. public interface vehicle {    default void print() {       System.out.println("I am a vehicle!");    }    static void blowHorn() {       System.out.println("Blowing horn!!!");    } }Default Method ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.javapublic class Java8Tester {    public static void main(String args[]) {       Vehicle vehicle = new Car(); vehicle.print();    } } interface Vehicle {    default void print() {   ...

Read More

How do I time a method execution in Java

Manikanth Mani
Manikanth Mani
Updated on 11-Mar-2026 1K+ Views

You should get a start time before making a call and end time after method execution. The difference is the time taken. Exampleimport java.util.Calendar; public class Tester {    public static void main(String[] args) {       long startTime = Calendar.getInstance().getTimeInMillis();       longRunningMethod();       long endTime = Calendar.getInstance().getTimeInMillis();       System.out.println("Time taken: " + (endTime - startTime) + " ms");    }    public static void longRunningMethod() {       try {          Thread.sleep(1000);       } catch (InterruptedException e) {          e.printStackTrace();       }    } }OutputTime taken: 1012 ms

Read More
Showing 1981–1990 of 5,881 articles
« Prev 1 197 198 199 200 201 589 Next »
Advertisements