Object Oriented Programming Articles

Page 453 of 589

Advantages and disadvantages of arrays in Java

Prabhas
Prabhas
Updated on 17-Jun-2020 2K+ Views

BenefitsEasier access to any element using the index.Easy to manipulate and store large data.DisadvantagesFixed size. Can not be increased or decrease once declared.Can store a single type of primitives only.

Read More

What are all the ways an object can be created in Java?

varun
varun
Updated on 16-Jun-2020 269 Views

You can create an objectUsing new keyword.Sample obj = new Sample();Using the newInstance() method and Class.forName() method.Sample obj2 = (Sample) Class.forName("Sample").newInstance();Using the clone() method by implementing Cloneable Interface (marker).Sample obj3 = (Sample) obj1.clone();Using class loader.Object obj4 = Sample.class.getClassLoader().loadClass("Sample");Using the constructor class from lang.reflect.Class cls = Sample.class; Constructor obj = cls.getDeclaredConstructors()[0]; Sample obj5 = (Sample) obj.newInstance();

Read More

How to count Java comments of a program that is stored in a text file?

Samual Sam
Samual Sam
Updated on 16-Jun-2020 606 Views

You can read the contents of a file using the Scanner class and You can find the comments in a particular line using contains() method.Exampleimport java.io.*; import java.util.Scanner; public class FindingComments {    public static void main(String[] args) throws IOException {       Scanner sc = new Scanner(new File("HelloWorld"));       String input;       int single = 0;       int multiLine = 0;       while (sc.hasNextLine()) {          input = sc.nextLine();          if (input.contains("/*")) {             multiLine ++;       ...

Read More

What are variadic functions in Java?

Monica Mona
Monica Mona
Updated on 16-Jun-2020 1K+ Views

Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions. Example public class Sample {     void demoMethod(String... args) {         for (String arg: args) {             System.out.println(arg);         }     }     public static void main(String args[] ){         new Sample().demoMethod("ram", "rahim", "robert");         new Sample().demoMethod("krishna", "kasyap");     } } Output ram rahim robert krishna kasyap

Read More

Java labelled for loop

Vikyath Ram
Vikyath Ram
Updated on 15-Jun-2020 2K+ Views

Following program is using labeled for loops. Example public class Tester {     public static void main(String args[]) {                first:            for (int i = 0; i 

Read More

Java labelled statement

Kumar Varma
Kumar Varma
Updated on 15-Jun-2020 4K+ Views

Yes. Java supports labeled statements. You can put a label before a for statement and use the break/continue controls to jump to that label. Example See the example below. public class Tester {    public static void main(String args[]) {      first:          for (int i = 0; i 

Read More

Java Conversions and Promotions

Paul Richard
Paul Richard
Updated on 15-Jun-2020 235 Views

We can convert one data types into another data type using casting. Narrowing ConversionNarrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Following program output will be 44.public class MyFirstJavaProgram { public static void main(String []args) { int a = 300; byte b = (byte)a; // narrowing System.out.println(b); } }Widening/Promotion ConversionWidening refers to passing a lower size data type like ...

Read More

Java overflow and underflow

Arjun Thakur
Arjun Thakur
Updated on 15-Jun-2020 4K+ Views

OverflowOverflow occurs when we assign such a value to a variable which is more than the maximum permissible value.UnderflowUnderflow occurs when we assign such a value to a variable which is less than the minimum permissible value.JVM does not throw any exception in case Overflow or underflow occurs, it simply changes the value. Its programmer responsibility to check the possibility of an overflow/underflow condition and act accordingly. Example (Overflow)Consider the case of int variable, it is of 32 bit and any value which is more than Integer.MAX_VALUE (2147483647) is rolled over. For example, Integer.MAX_VALUE + 1 returns -2147483648 (Integer.MIN_VALUE).As int ...

Read More

Java variable declaration best practices

Fendadis John
Fendadis John
Updated on 15-Jun-2020 2K+ Views

Following are the best practices while declaring a variable.Variables names should be short or long enough as per the scope. For example, loop counter variable, i is fine whereas employee as a loop variable.Specific words should not be used as equals, compare, data.Use meaningful names which can explain the purpose of the variable. For example cnt Vs counter.Don't use _ to declare a variable name, Use camel casing. For example, employeeName is better than employee_name.Each organization has its own syntax specific standards. Follow those rules to maintain consistency and readability.

Read More

Why is Java slower than C++ programs?

Akshaya Akki
Akshaya Akki
Updated on 13-Jun-2020 1K+ Views

Modern Java is quite fast and is comparable to C++ code base but it still takes lot of memory. Slowness of Java programs is primarily because of bad programming practices. But following areas are where Java can be improved.Java libraries are written keeping readability and correctness in mind, not performance.Slow String based operations as Strings are UTF-16 encoded objects and are immutable. So more String are used, more memory is required.Boundary checks on arrays also make its operations bit slow.I/O Stream operations are slow considering synchronization checks on each access.Lacking low level functionality like C also attributes to slowness in ...

Read More
Showing 4521–4530 of 5,881 articles
« Prev 1 451 452 453 454 455 589 Next »
Advertisements