Found 9320 Articles for Object Oriented Programming

What is the lambda expression to convert array/List of String to array/List of Integers in java?

Srinivas Gorla
Updated on 16-Jun-2020 10:12:36

615 Views

You can convert an array list of strings to an array list of integers as:ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class LamdaExpressions {    public static void main(String args[]) {       ArrayList list = new ArrayList();       list.add("123");       list.add("223");       list.add("323");       list.add("334");       List listInteger =          list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());       System.out.println(listInteger);    } }Output[123, 223, 323, 334]

How can I put a Java arrays inside an array?

Abhinanda Shri
Updated on 16-Jun-2020 10:11:04

2K+ Views

ExampleLive Demoimport java.util.Arrays; public class ArrayWithinAnArray{    public static void main(String args[]) {       int[] myArray1 = {23, 56, 78, 91};       int[] myArray2 = {123, 156, 178, 191};       int[] myArray3 = {223, 256, 278, 291};       int[] myArray4 = {323, 356, 378, 391};       int [][] arrayOfArrays = {myArray1, myArray2, myArray3, myArray4};       System.out.println(Arrays.deepToString(arrayOfArrays));    } }Output[[23, 56, 78, 91], [123, 156, 178, 191], [223, 256, 278, 291], [323, 356, 378, 391]]

Why are classes sometimes declared final in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:20

536 Views

If a class is declared final, you cannot inherit it. If you try it gives you a compile-time error as − Example final class Super { private int data = 30; } public class Sub extends Sub { public static void main(String args[]){ } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Sub.main(Sub.java:7)

Are the private variables and private methods of a parent class inherited by the child class in Java?

varma
Updated on 30-Jul-2019 22:30:20

702 Views

No, a child class can’t inherit private members of the parent class, it can inherit only protected, default, and public members of it. If you try it gives you a compile time error as: − Example class Super{ private int data = 30; public void display(){ System.out.println("Hello this is the method of the super class"); } } public class Sub extends Super{ public void greet(){ System.out.println("Hello this is the method of the sub class"); ... Read More

What are Java parent and child classes in Java?

usharani
Updated on 16-Jun-2020 08:47:34

14K+ Views

Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another.You can inherit the members of one class from another, use the extends keyword as:class A extends B{}The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).Following is an example which demonstrates inheritance. Here, we have two classes namely Sample and MyClass. Where Sample is the parent class and the class named MyClass is the child class.ExampleLive Democlass Sample{    public ... Read More

What are static members of a Java class?

varun
Updated on 30-Jul-2019 22:30:20

22K+ Views

In Java, static members are those which belongs to the class and you can access these members without instantiating the class. The static keyword can be used with methods, fields, classes (inner/nested), blocks. Static Methods − You can create a static method by using the keyword static. Static methods can access only static fields, methods. To access static methods there is no need to instantiate the class, you can do it just using the class name as − Example Live Demo public class MyClass { public static void sample(){ ... Read More

How to declare, define and call a method in Java?

Prabhas
Updated on 30-Jul-2019 22:30:20

2K+ Views

Following is the syntax to declare a method in Java. Syntax modifier return_type method_name(parameters_list){ //method body } Where, modifier − It defines the access type of the method and it is optional to use. return_type − Method may return a value. method_name − This is the method name. The method signature consists of the method name and the parameter list. parameters_list − The list of parameters, it is the type, order, and a number of parameters of a method. These are optional, method may contain zero parameters. method body − The method body defines ... Read More

What is the difference between simple name, canonical name and class name in a Java class?

vanithasree
Updated on 30-Jul-2019 22:30:20

2K+ Views

Canonical name of a Java class is the name of the class along with the package. For example, the canonical name of the class File is java.io.File. You can also get the canonical name of a particular class using Java method. The class named Class provides a method getCanonicalName(), this method returns canonical name of the current class. Example Live Demo import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); ... Read More

How to access the members of a class from another class in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:20

11K+ Views

To access the members of a class from other class. First of all, import the class. Create an object of that class. Using this object access, the members of that class. Suppose there is a class in a package called myPackage with a method named display() package myPackage; public class Sample { public void display() { System.out.println("Hello"); } } You can access it as. import myPackage; public class MyClass { public static void main(String args[]) { Sample s = new Sample(); s.done(); } } Output hello

Why do Java array declarations use curly brackets?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

483 Views

Curly brackets usually denote sets and ensembles while parenthesis usually in most Algol-based programming languages curly braces are used to declare arrays.

Advertisements