Skip to main content

Posts

Showing posts from January, 2023

Program to Prove String is immutable and String Builder is Mutable

In our last article , we understood difference in String vs String Builder vs String Buffer  , as you know String is immutable and String Builder and String Buffer is mutable ,see below example to understand concept clearly : public class StringPractice { public static void main(String[] args) { String name="Swapnil"; System.out.println("original name "+name); System.out.println("modifying name as "+name.concat("Vyawhare")); System.out.println("*********************final name using String : "+name); StringBuilder name2=new StringBuilder("Swapnil"); System.out.println("original name "+name2); System.out.println("modifying name as "+name2.append("Vyawhare")); System.out.println("*********************final name using StringBuilder :"+name2); StringBuffer name3=new StringBuffer("Swapnil"); System.out.println("original name "+name3); Sy...

String vs StringBuffer vs StringBuilder

String is immutable whereas StringBuffer and StringBuilder are mutable classes. String is one of the most widely used classes in Java. StringBuffer and StringBuilder classes provide methods to manipulate strings. Since String is immutable in Java,whenever we do String manipulation like concatenation, sub-string, etc.it generates a new String.So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation. StringBuffer vs StringBuilder StringBuffers all public methods are synchronized. StringBuffer provides Thread safety but at a performance cost , it reduce performance. In most of the scenarios, we don’t use String in a multithreaded environment. So Java 1.5 introduced a new class StringBuilder,  which is similar to StringBuffer except for thread-safety and synchronization. StringBuffer has some extra methods such as substring, length, capacity, trimToSize, etc. However,these are not required since you have all these present in String too. ...

String In Java

String class available in  java.lang package . java.lang.String String in java is defined as , an object that represents sequence of characters.In java , objects of String are immutable which means a constant , and can not be changed once created. String class provide various important method like :  toUpperCase() - convert String to upper case. toLowerCase()  - convert String to lower case. length() - to check length of String. concat() - to concatenate two String . split() - to split String. and many other methods ,each method is used to perform some operation on String value. Ways To Create String in Java : check below program where we are creating String type variable using different ways : public class StringDemo { public static void main(String[] args) { String websiteUrl="https://www.crtr4u.com"; String subject=new String("java programming"); char charArray[] = {'P','Q','R','S','T'}; String strFromCharArray...

Java 8 Features Part -1

Java 8 introduced benefits of Functional Programming in java. Lambda Expression :  Lambda is an anonymous function in java , a function without name ,return type and access modifier. In java , normally we write method as below : Example :Without Lambda public void run(){ System.out.println("Welcome to www.crtr4u.com"); } using lambda expression same method can be written as:      ()->System.out.println("Welcome to www.crtr4u.com"); if we have multiple statements then we can use brackets, Lambda Expression also known as anonymous function or closures. Example : without using lambda : public void add(int num1,int num2){ System.out.println(num1+num2); } above example can be written using lambda expression as : (int num1,int num2)->System.out.println(num1+num2); Why To Use Lambda Expression :  1.Easy to implement and Less Code as shown in above example.  2.We can pass lambda expression as paramater to other method. To implement Lambda Expr...

Java Interview Topic - How To Create Thread In Java ?

 How To Create Thread In Java ? We can create thread in two ways : 1.By Extending Thread Class 2.By Implementing Runnable Interface. Example of Creating Thread By Extending Thread class : public class MyThread extends Thread { public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.start(); } @Override public void run() { System.out.println("Thread 1 is started"); } } Output: Thread 1 is started Example of Creating Thread by implementing Runnable Interface : public class ThreadDemo implements Runnable { public static void main(String[] args) { ThreadDemo td=new ThreadDemo(); Thread myThread=new Thread(td); myThread.start(); } @Override public void run() { System.out.println("Thread is started"); } } Output:   Thread is started

What is Method In Java

As we know Object is an entity with states and behavior , in java we are creating methods to represent behavior of object. Method is defined as group of instructions grouped together to perform a task, one of the biggest advantage of method is re-usability.we can create a method in java and we can call that method many times to perform some task.We do not need to write same code again and again.We can execute a method by calling or invoking it. One of the most important method in java is the main method ,execution of every java program , starts with main method : How to write main method in java ? public static void main(String[] args) { } Note - execution of every java program starts with main method. - we can create our own method to perform some set of operations : Method Signature In Java : syntax : AccessSpecifier ReturnType methodName (parameter1,parameter2){ } NOTE - Method Name should be in Camel Case Format : Example : addNumbers(),calculateInterest(),addition(), getEm...

this keyword in java

 this keyword in java refers to current object, where to use this keyword in java ? this keyword in java having multiple benefits and usage ,lets see one by one : 1.To Refer Current class instance variables   we can use this keyword in java Example :  step 1: firstly create a class Website.java as below  class Website{ String authorName; String websiteUrl; public Website(String authorName, String websiteUrl) { this.authorName = authorName; this.websiteUrl = websiteUrl; } public String getAuthorName() { return authorName; } public void setAuthorName(String authorName) { this.authorName = authorName; } public String getWebsiteUrl() { return websiteUrl; } public void setWebsiteUrl(String websiteUrl) { this.websiteUrl = websiteUrl; } @Override public String toString() { return "Website [authorName=" + authorName + ", websiteUrl=" + websiteUrl + "]"; } } Step 2 : create a class W...

Create Blog Step By Step

Hi everyone , welcome to our channel , today we are going to learn how to create a blog step by step : Follow this video step by step , and enjoy amazing Blogger platform provided by google. to earn money from your blog follow below steps : STEP 1: Publish high quality articles 40-50. STEP 2: Apply for Adsense approval. STEP 3: Google Adsense will publsh advertisements on your blog for which you will get paid. thanks a lot for visiting our blog.

Introduction to Java

Java is both a programming language and a platform , It is a high level language that is  simple ,object oriented, distributed, multithreaded, dynamic, architecture neutral, portable, robust  and secure. In the Java programming language, all source code is first written in plain text  files ending with the .java extension. Those source files are then compiled into .class files  by the Java compiler (javac). A .class file does not contain code that is native to a processor;  it instead contains bytecodes - the machine language of the Java Virtual Machine. The Java launcher tool (java) then runs the application with an instance of the Java Virtual Machine. The Java Virtual Machine is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris Operating System (Solaris OS), Linux, or MacOS. Some virtual machines,such as the Java HotSpot Virtual Machine, perform additional steps at runtime t...

The Network Adapter could not establish the connection | Listener service on Local Computer started and then stopped | issue solved

we always face below issue while connecting sql developer to oracle database : The Network Adapter could not establish the connection | Listener service started and stopped, Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=kcb5ehuwRySZzT/ZFcnkow==), oracle Sql developer connection issue, Step by Step Solution for above problem is  :  click here to get solution for The Network Adapter could not establish the connection Network Adapter issue is quite common for all , we can fix issue easily by following this video , if any question you can comment below , but firstly watch video try to resolve issue on your own and if you get failure then i will help you remotely via google meet. thanks for visiting our blog.