100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Examen

Exam (elaborations) Design and analysis of algorithm

Puntuación
-
Vendido
-
Páginas
19
Grado
A
Subido en
08-09-2024
Escrito en
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.

Mostrar más Leer menos










Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Información del documento

Subido en
8 de septiembre de 2024
Número de páginas
19
Escrito en
2024/2025
Tipo
Examen
Contiene
Solo preguntas

Temas

Vista previa del contenido

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
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
isaacfrp1998

Conoce al vendedor

Seller avatar
isaacfrp1998 Exam Questions
Ver perfil
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
0
Miembro desde
1 año
Número de seguidores
0
Documentos
30
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes