Tag: string

5 Must to know differences in java

Java
5 Must to know differences in java: Being a java developer, you must know this 5 differences, because which tells about your correct and deep understanding of this programming language, as well as it is very basic to know in java. 1. As everyone know String is the heart part in java. Because very difficult to write a simple utility/business logic without a string variable. For dynamic search query constructions we use StringBuilder/StringBuffer. So knowing the differences between String, StringBuilder and StringBuffer helps you to write a correct proper logic at correct place.   Difference between String/StringBuilder and StringBuffer   2. If you are in beginning stage of java learning/java logic implementations then knowing about the equals() and == save lot o...

All Date Type Conversions in Java with Examples

Java
All Date Type Conversions in Java with Examples: This article "All Date Type Conversions in Java with Examples" we are writing to help people to quickly find the data type conversions in java with examples. Few of the below conversions are many times asked in interviews also. So bookmark and read often to store it in your mind.   Array to ArrayList convert in Java Example:   Here we are just iterating the states array and adding it to the arraylist.   Array to ArrayList Conversions Java Program:¬† [java] package in.javadomain;java.util.ArrayList; public class ArrayToArrayList { public static void main(String[] args) { ArrayList<String> statesArrayList = new ArrayList<String>(); String[] statesArray = new String[] { "Chennai", "Mumbai", "Delhi"

String is Immutable, what does it mean ?

Java
String is Immutable, what does it mean ? String is immutable: String objects in java is immutable. Meaning of immutable is it can not be changed or altered. In java, strings are immutable which means that string objects can not be modified or changed, instead¬†new object will be created. Eg: [java] public class Javadomain{ public static void main(String[] args) { String myString = "Java"; myString = "domain"; System.out.println("myString is :::"+myString); } } [/java]   Output:   myString is :::domain     Explanation: Here initially we have assigned a "Java" to the string referece myString. Later we have assigned "domain" to the same string reference myString. reference is changed, but string obje

Converting String Array to Integer Array Example Java

Java
Java Program to Convert String Array to Integer Array: In this below program we have converted string array to integer array. [java gutter="false"] package in.javadomain; public class StringArrayToIntArray { public static void main(String[] args) { String[] strArray = { "10", "20", "30", "40", "50" }; int[] intArray = new int[strArray.length]; for (int i = 0; i < strArray.length; i++) { intArray[i] = Integer.parseInt(strArray[i]); } for (int i = 0; i < intArray.length; i++) { System.out.println("Integer Array Values :: " + intArray[i]); } } } [/java] Output: [plain gutter="false"] Integer Array Values :: 10 Integer Array Values :: 20 Integer Array Values :: 30 Integer A...

Split in Java Example

Java
Split Function:   You can split a string using regex(regular expression) or by comma or anything you want. Here example given to split a string by comma. String Split by Comma Java Program: [java] package in.javadomain; import java.util.ArrayList; public class CommaSplit { public static void main(String[] args) { String links = "http://www.google.com,http://www.facebook.com,http://www.youtube.com,http://www.ebay.com"; ArrayList linkList = new ArrayList(); // Splitting by comma and storing in string array String[] linkSplit = links.split(","); for (int i = 0; i < linkSplit.length; i++) { linkList.add(linkSplit[i]); } // Printing ArrayList values for (Object link : linkList) { System.out.println(link); }...

Taking only String values from ArrayList

Java
Scenario: You are given with a arraylist with all datatype values. Eg: Integer and String values. But you want to move only string values to another arraylist. Solution you can think suddenly: ArrayList can be declared to hold only string and using addAll method we can move. Eg: ArrayList arStr = new ArrayList(); arStr.addAll("given_arrayList"); But the above one does not throw any error as well which will move all the values(String and Integer) to another list. If you try to move using add method which will throw casting exception. So How can we do ? [java highlight="17,18"] package in.javadomain; import java.util.ArrayList; public class StringOnlyAL{ public static void main(String[] args) { ArrayList alAll = new ArrayList(); alAll.add("Java"...

Truncate String in Java Example

Java
Removing First Few Characters Java Program: [java] package in.javadomain; public class Truncate { public static void main(String[] args) { String input ="www.Javadomain.in"; System.out.println("String: "+input); String truncateFirst = removeFirst(input); System.out.println("Truncating First 4 characters: "+truncateFirst); } private static String removeFirst(String input){ return input.substring(4,input.length()); } } [/java] Output: [plain] String: www.Javadomain.in Truncating First 4 characters: Javadomain.in [/plain]     Removing Last Few Characters Java Program: [java] package in.javadomain; public class Truncate { public static void main(String[] args) { String input ="...

String Reverse in Java Example

Java
String Reverse Java Program: [java] package in.javadomain; public class stringReverse { public static void main(String[] args) { String input = "DOG"; System.out.println("Original String :::" + input); StringBuffer reverseInput = new StringBuffer(input).reverse(); System.out.println("Reversed String :::" + reverseInput); } } [/java] Output: [plain] Original String :::DOG Reversed String :::GOD [/plain]

Difference between concat and append in Java

Java
Difference between concat and append in java: concat(): String has concat method, remember string is immutable. It adds a string to another string. It will create the new object after concatenation done, since it is a immutable. append(): StringBuilder and StringBuffer has append method, remember these two are mutable. It appends a char or char sequence to a string. It will not create a new object, since it is a mutable one. Example Program: [java] package com.ngdeveloper.com; public class StringConcatenation { public static void main(String[] args) { //Concatenating string with another string String helloVariable = "hello "; String worldVariable = "world"; System.out.println(helloVariable.concat(worldVariable)); //hello world //Appending str...

Difference between concat and + in java

Java
Difference between + and concat(): Using + operator: 1. Any type of variable can be concatenated, it is not mandatory to be a string variable. [java] String firstVar = "Java"; int secVar = 11; String plusConcat = firstVar + secVar; System.out.println(plusConcat); // prints Java11 [/java] 2. String can be null and it will not throw null pointer exception. [java] String myVar = null; String plus = myVar + "java"; System.out.println(plus); // prints nulljava [/java]    Using Concat():   1. Argument must be string for concatenation using concat() [java] a.concat(11) //it is not possible [/java] 2. String must not be null, if it is null then it will throw null pointer exception. [java] String myVar = null; String valu...