100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Exam (elaborations)

Exam (elaborations) Design and analysis of algorithm

Rating
-
Sold
-
Pages
19
Grade
A
Uploaded on
08-09-2024
Written in
2024/2025

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 languages, software engineering, artificial intelligence, machine learning, database management, and cybersecurity. Each document is expertly structured to facilitate easy understanding and retention, making them an invaluable resource for students and professionals alike. Whether you're preparing for exams, working on assignments, or seeking to deepen your knowledge, these documents provide clear explanations, practical examples, and insightful analysis that will enhance your learning experience. Don't miss out on this opportunity to access high-quality, reliable study materials that are just a click away.

Show more Read less










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
September 8, 2024
Number of pages
19
Written in
2024/2025
Type
Exam (elaborations)
Contains
Only questions

Subjects

Content preview

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
$2.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
isaacfrp1998

Get to know the seller

Seller avatar
isaacfrp1998 Exam Questions
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
30
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions