100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Exam (elaborations) Design and analysis of algorithm $2.99   Add to cart

Exam (elaborations)

Exam (elaborations) Design and analysis of algorithm

 4 views  0 purchase

Unlock a comprehensive and meticulously crafted collection of computer science documents available for sale on Stuvia. These documents encompass a wide array of topics fundamental to both introductory and advanced levels of computer science, including algorithms, data structures, programming langua...

[Show more]

Preview 3 out of 19  pages

  • September 8, 2024
  • 19
  • 2024/2025
  • Exam (elaborations)
  • Only questions
All documents for this subject (89)
avatar-seller
isaacfrp1998
OBJECT ORIENTED PROGRAMMING USING JAVA



1. How do you compare two strings in Java? Explain with example.
We can compare String in Java on the basis of content and reference.
It is used in authentication (by equals() method), sorting (by compareTo() method), reference
matching (by == operator) etc.
There are three ways to compare String in Java:
By Using equals() Method
By Using == Operator
By compareTo() Method


By Using equals() Method
The String class equals() method compares the original content of the string. It compares values of string
for equality.

class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2)); true
System.out.println(s1.equals(s3)); true
System.out.println(s1.equals(s4)); false
}
}


By Using == Operator
The == operator compares references not values.
class Teststringcomparison3{

,public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2); true (because both refer to same instance)
System.out.println(s1==s3); false(because s3 refers to instance created in nonpool)
}
}


By compareTo() Method
The String class compareTo() method compares values lexicographically and returns an integer
value that describes if first string is less than, equal to or greater than second string.
Suppose s1 and s2 are two String objects. If:
s1 == s2 : The method returns 0.
s1 > s2 : The method returns a positive value.
s1 < s2 : The method returns a negative value.
class Teststringcomparison4{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2)); 0
System.out.println(s1.compareTo(s3)); 1(because s1>s3)
System.out.println(s3.compareTo(s1)); -1(because s3 < s1)
}
}

, 2. How to declare a string in Java? Explain with example.
String is a sequence of characters. But in Java, string is an object that represents a sequence of
characters. The java.lang.String class is used to create a string object.

There are two ways to create String object:

1. By string literal
2. By new keyword

By String Literal

Java String literal is created by using double quotes. For Example:

String s="welcome";

Each time you create a string literal, the JVM checks the "string constant pool" first. If the string
already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist
in the pool, a new string instance is created and placed in the pool. For example:

String s1="Welcome";
String s2="Welcome";//It doesn't create a new instance




In the above example, only one object will be created. Firstly, JVM will not find any string object
with the value "Welcome" in string constant pool that is why it will create a new object. After that

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller isaacfrp1998. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $2.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77254 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$2.99
  • (0)
  Add to cart