Found 271 Articles for Java8

How to test if a Java String contains a case insensitive regex pattern

Arnab Chakraborty
Updated on 24-Jun-2020 07:29:15

204 Views

the syntax? i:x makes the string search case-insensitive. for egpublic class RegCaseSense {    public static void main(String[] args) {       String stringSearch = "HI we are at java class.";       // this won't work because the pattern is in upper-case       System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*"));         // the magic (?i:X) syntax makes this search case-insensitive, so it returns true       System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)"));    } }

Constructor overloading in Java

Sravani S
Updated on 05-Mar-2020 12:22:53

5K+ Views

Yes! Java supports constructor overloading. In constructor loading, we create multiple constructors with the same name but with different parameters types or with different no of parameters.ExampleLive Demopublic class Tester {      private String message;      public Tester(){       message = "Hello World!";    }    public Tester(String message){       this.message = message;    }      public String getMessage(){       return message ;    }      public void setMessage(String message){       this.message = message;    }      public static void main(String[] args) {       Tester tester = new Tester();       System.out.println(tester.getMessage());           Tester tester1 = new Tester("Welcome");       System.out.println(tester1.getMessage());      } }   OutputHello World! Welcome

Anonymous object in Java

Janani Jaganathan
Updated on 25-Aug-2022 10:00:49

11K+ Views

Anonymous object in Java means creating an object without any reference variable. Generally, when creating an object in Java, you need to assign a name to the object. But the anonymous object in Java allows you to create an object without any name assigned to that object. So, if you want to create only one object in a class, then the anonymous object would be a good approach. Reading this article, you will learn what an anonymous object is and how to create and use anonymous objects in Java. Let's get started! Anonymous Object in Java Anonymous means Nameless. An ... Read More

Creating multiple Java objects by one type only

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

1K+ Views

You can create a List of object easily. Consider the following example, where I'll create an array of Employee objects and print their details in a for loop. import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; public class Tester implements Cloneable { private int data; public int getData() { return data; } public void setData(int data) { this.data = data; } public Tester(int data){ ... Read More

5 different ways to create objects in Java

Nikitha N
Updated on 06-Mar-2020 06:12:45

4K+ Views

Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following five ways:1. Using new keyword.Tester tester1 = new Tester();2. Using Class.forName() methodTester tester2 = (Tester)Class.forName("Tester").newInstance();3. Using clone method.Tester tester3 = tester1.clone();4. Using Constructor.forName() methodTester tester4 = Tester.class.getConstructor().newInstance();5. Using DeserializationObjectInputStream objectInputStream = new ObjectInputStream(inputStream ); Tester tester5 = (MyObject) objectInputStream.readObject(); Using new keyword is the most preferred one.

3 ways to initialize an object in Java

Syed Javed
Updated on 06-Mar-2020 06:09:29

7K+ Views

Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following three ways −1. Using new keyword.Tester tester1 = new Tester();2. Using Class.forName() methodTester tester4 = (Tester)Class.forName("Tester").newInstance();3. Using clone method.Tester tester2 = tester1.clone();

New keyword in Java

Nikitha N
Updated on 06-Mar-2020 06:08:28

535 Views

Yes, it is similar to a new keyword of C++. a new keyword is used to initialize/create an object. See the following example −Employee employee = new Employee();Here new keyword is used to create an object of class Employee.new Employee() invokes the constructor of the class Employee.new keyword can also be used without assigning the object to a reference variable. See the example −String name = new Employee().getName();Here we are creating an object using new keyword and then invoked a method getName() on the object and passed the result to a variable.

CamelCase in Java naming conventions

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

713 Views

Java follows camel casing for objects, class, variables etc. If a name is having multiple words, the first letter is small then consecutive words are joint with the first letter as a capital case. Consider the following example − Taxation Department Class - TaxationDepartment Object - taxationDepartment Method - getTaxationDepartmentDetails Variable - taxationDepartment

Advantages of naming conventions in Java

Syed Javed
Updated on 06-Mar-2020 05:38:32

318 Views

Following the the best practices while declaring a variable.  These best practices maintains code readability, understandability as project code size increases.Variables names should be short or long enough as per the scope. For example, loop counter variable, i is fine whereas employee as loop variable.Specific words should not be used like 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 ... Read More

Java Naming conventions

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

2K+ Views

All Java components require names. Names used for classes, variables and methods are called identifiers. In Java, there are several points to remember about identifiers. They are as follows - All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters. A keyword cannot be used as an identifier. Most importantly, identifiers are case sensitive. Examples of legal identifiers: age, $salary, _value, __1_value. Examples of illegal identifiers: 123abc, -salary. A class name should start from a capital case ... Read More

Advertisements