CS506 Assignment 1 Complete Solution || CS 506 Assignemtn 2021
Employee.java
public class Employee {
private int empid;
private String empName;
//vuportal.com
private int empExperience;
private String qualification;
private int Salary;
private String dept;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
Lecture 3: Learning Basics
3.1 Strings A string is commonly considered to be a sequence of characters stored in memory and accessible as a unit. Strings in java are represented as objects.
3.1.1 String Concatenation
• “+” operator is used to concatenate strings o System.out.println(“Hello” + “World”) will print Hello World on console • String concatenated with any other data type such as int will also convert that datatype to String and the result will be a concatenated String displayed on console. For example, o int i = 4; o int j = 5; System.out.println (“Hello” + i);// will print Hello 4 on screen o However
System.out.println(i+j);//will print 9 on the console because both i and j are of type int.
3.1.2 Comparing Strings
For comparing Strings never use == operator, use equals method of String class. • == operator compares addresses (shallow comparison) while equals compares values (deep comparison)
• E.g. string1.equals(string2)
Example Code: String concatenation and comparison public class StringTest { public static void main(String[] args) { int i = 4; int j = 5; System.out.println("Hello" + i); // will print Hello4 System.out.println(i + j); // will print 9 String s1 = new String (“pakistan”);
String s2 = “pakistan”; if (s1 == s2) { System.out.println(“comparing string using == operator”); } if (s1.equals( s2) ) { System.out.println(“comparing string using equal method”); } } }
On execution of the above program, following output will produce .
Posting Komentar untuk "CS506 Assignment 1 Complete Solution || CS 506 Assignemtn 2021"