Thursday 2 July 2020

JavaScript operators | Types of operator in javascript - Online Help

Hello, readers today we are going to see what is javascript operators and types of the operator in detail. So let's gets started.


JavaScript operators | Types of operator in javascript - Online Help

Operators in javascript:

An operator is a symbol that tells the computer to perform certain mathematical and logical operations. Operators are used in a program to manipulate data and variables. 


Types of the operator in JavaScript:

The JavaScript language following type of operators:
  • Arithmetic operator
  • Comparison operator
  • Logical or relational operator
  • Assignment operator
  • Conditional or ternary operator
  • Bitwise operator

Let’s have a look on all operators one by one.


Arithmetical operator:

There are following arithmetical operator supported by JavaScript language:

Assume variable A holds 10 and variable B holds 20 then:

 Operator Description Example
 +  Adds two operands A+B will give 30
 - Subtract the second operand from the first  A-B will give -10
 * Multiply both operands A*B will give 200
 / Divide numerator by denominator  B/A will give 2
 %  Modulus operator and a reminder of after an integer division B%A will give 0
 ++  Increment operator, increases integer value by one.  A++ will give 11
 --  The decrement operator, decreases the integer value by one. A—will give 9

Example:

<html>
<head>
<title> Javascript</title>
</head>
<body>
<H2>
<script type=”text/javascript”>
var a=8;
var b=1;
document.write(“<h2>”+a+”+“+b+”=”+(a+b)+”<br></h2>”);
document.write(“<h2>”+a+”-“+b+”=”+(a-b)+”<br></h2>”);
document.write(“<h2>”+a+”*“+b+”=”+(a*b)+”<br></h2>”);
document.write(“<h2>”+a+”/“+b+”=”+(a/b)+”<br></h2>”);
</script>
</h2>
</body>
</html>

The output of the above program:
8+1=9
8-1=7
8*1=8
8/1=8


Javascript comparison operator:

There are following comparison operator supported by JavaScript language:

Assume variable A hold 10 and variable B holds 20 then:

 Operator Description Example
 == Checks if the value of two operands are equal or not, if yes then condition becomes true. (A==B) is not true
 != Checks if the value of two operands are equal or not, if the value is not equal then the condition becomes true.(A!=B) is true
 > Checks if the value of the left operands is greater than the value of right operand, if yes then condition becomes true.  (A>B) is not true
 < Checks if the value of the left operand is less than the value of right operand, if yes then the condition becomes true. (A<B) is true
 >= Checks if the value of the left operand is greater than or equal to the left operand, if yes then the condition is true. (A>=B) is not true
 <= Checks if the value of the right operand is less than or equal to the value of right operand, if yes then the condition is true.  (A<=B) is true

Example:

<html>
<head><title>Comparison operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var a=200;
var b=300;
if(a>b)
document.write(“A is greater than B”);
else
document.write(“B is greater than A”);
</script>
</h2>
</body>
</html>


Logical operator:

These are the following logical operators supported by javascript.

Assume that variable  A holds 10 and variable  B holds 20 then:

 Operators Description Example
 && && is called a logical AND operator. If both the operands are non-zero then the condition becomes true.  (A&&B) is true
 || || is called a logical OR operator. If any of the two operands are non zero then the condition becomes true (A||B) is true
 ! ! is called logical NOT. Use to reverse the logical state of its operands. If a condition is true then the logical NOT operator will make false. !(A&&B) is false

Example:

<html>
<head><title>Logical operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var marks1=24;
var marks2=35;
if(mark1>40 && marks2=>40)
document.write(“ pass”);
else
document.write(“fail”);
</script>
</h2>
</body>
</html>
The output is:
fail


Bitwise operator:

These are the following bitwise operator supported by the javascript language.

Assume that the variable A holds 2 and variable B holds 3 then:

 Operator Description Example
 & Called bitwise AND operator. It performs Boolean AND operation on each bit of its integer argument.

 (A&B) is 2
 |  Called bitwise OR operator. It performs Boolean OR operation on each bit of its argument. (A|B) is 3
 ^ Called bitwise XOR operator. It performs a Boolean exclusive OR operation on each biut of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. (A^B) is 1
 ~ Called bitwise NOT operator. It is a unary operator and operates by reversing all bits in the operands (~B) is -4
 << Called bitwise shift left operator.it moves all bits in its first operand to the left by the number of places specified in the second operand.

New bits are filled with zero. shifting a value left by one position is equivalently multiplying by 2, shifting 2 positions is equivalent to multiplying by 4, etc.,
 (A<<1) is 4
 >> Called bitwise shift right with sign operator.it moves all the operands to the right by the number of places specified in the second operand.

The bits filled in on the left depends on the sign bit of the original operand, in order to preserve the sign of the result.

If the first operand is positive, the result has zeroes placed in the high bits; if the first operands are negative, the result has ones placed in the high bit.

 Shifting a value right one place is equivalent to dividing by 2(discarding the reminder), shift right two places is equivalent to integer division by 4, and so on
 (A>>1) is 1
 >>>  This operator is called a bitwise shift right with zero operators. This operator is just like the >> operator, except that the bits shifted in on the left are always zero,  (A>>>1) is 1


Assignment operator:

These are the following assignment operators supported by the javascript language:

 Operator  Description Example
 =  A simple assignment operator assigns a value from right side operands to left side operand
C=A+B will assign the value of A+B into C
 +=  Add AND assignment operator, it adds right operands to left operands and assigns the result to the left operand. C+=A is equivalent to C=C+A
 -= Subtract AND assignment operator, it subtracts right operands to left operands and assigns the result to the left operand.C-=A is equivalent to C=C-A
 *= Multiply AND assignment operator, it multiplies right operands to left operands and assigns the result to the left operand.C*=A is equivalent to C=C*A
 /= Divide AND assignment operator, it divides right operands to left operands and assigns the result to the left operand.C/=A is equivalent to C=C/A
 %= Modulus AND assignment operator takes modulus using two operands and assigns the result to the left operand.C%=A is equivalent to C=C%A

Example:

<html>
<head><title>Assignment operator</title></head>
<body>
<script type=”text/javascript”>
var a=1980;
var b=1985;
document.write(a+”++”+b+”=”+(a==b)+”<br>”);
document.write(a+”>=”+b+”=”+(a>=b)+”<br>”);
document.write(a+”<=”+b+”=”+(a<=b)+”<br>”);
</script>
</body>
</html>

The output is:
1980==1985=false
1980>=1985=false
1980<=1985=true


Ternary operator javascript/conditional operator:

There is an operator called a conditional operator. It is also called a ternary operator. this operator first evaluates an expression for a true or false value and then execute one of the two given statement depending upon the result of the evaluation. The conditional operator has this syntax:

 Operator description Example
 ?:Conditional expression  
If the condition is true? Then value X: Otherwise value Y


Example:

<html>
<head><title>Logical operator</title></head>
<body>
<h2>
<script type=”text/javascript”>
var a=24;
var b=20;
document.write(a>b ? ”true”: “false”);
</script>
</h2>
</body>
</html>

The output is:
true

I hope you guyz understand what is javascript operators and types of operators in detail. If you have any queries or you guys find any mistake then please comment down below. Thank you for reading.


Recommended post:

Thursday 25 June 2020

Advantages of Javascript - Online Help

Advantages of JavaScript:

 The following are the advantages of JavaScript:

  • JavaScript is relatively secure
  •  It is widely supported in browsers
  •  It gives easy access to document object and can manipulate most of them
  •  JavaScript can give interesting animations with many multimedia data types
  •  The special plugin is not required to use JavaScript
  •  JavaScript is a secure language
  •  JavaScript code resembles the code of C language the syntax of both the language is very close to each other. The set of tokens and constructs are the same in both the language
  •  Less server interaction
  •  Immediate feedback to the visitors
  •  Increased interactivity
  •  Richer interfaces
  •  The web server doesn't need a special plugin to use your scripts

JavaScript Syntax | General form of JavaScript - Online help

JavaScript syntax:

JavaScript Syntax is embedded into an HTML file. A  browser reads HTML files And interprets HTML tags. Since be included as an integral part of an HTML document when required, the browser needs to be informed that specific code is javascript. The browser will then use its built-in JavaScript engine to interpret this code.

JavaScript Syntax | General form of JavaScript - Online help

A JavaScript consists of JavaScript statements that are placed within the <script>................</script> HTML tags in a web page. The script tag takes two important attributes: 

Attributes    Description
Language
  • Indicates the scripting language used for writing the snippet of the scripting code.
  • If left undefined Netscape communicator will assume JavaScript.
  • If left undefined Internet Explorer will assume VB script.
Type 
This attribute is what is now recommended to indicate the scripting language in use and its value should be set to “text/javaScript”.


The syntax of your JavaScript will be as follows:

<script language=”javascript”>
Javascript code
</script>

Or

<script type=”text/javascript”>
Javascript code
</script>

The <script> tag alerts the browser program to begin interpreting all the text between these tags as a script.

Statement is a line of code. This has to be ended with a semicolon(;).

Block is a set of statements put into a pair of brackets. ({,})

Comments:
-Single line comment starts by //.
-Multiline comments start with /* and end with */



Embedding JavaScript in HTML file:

If we are writing small scripts, or only use our scripts in a few pages, then the easiest way is to include the script in the HTML code. 

General syntax of embedding javascript in HTML:

<html>
<head>
<script language=”javascript”>
<! - -
Javascript code
// - ->
</head>
<body>
………….
………..
…………
</body>
</html>

Example:

<html>
<head>
<title>sample program</title>
</head>
<body>
<script type=”text/javascript”>
document.write(“hello readers”);
</script>
</body>
</html>

The above code will display the following result:
hello readers

Explanation of code:

The <script> tag tells the browser to expect a script in between them. You specify the language using the type attribute. The most popular scripting language on the web is JavaScript.

The part that writes the actual text is only one line (document.write(“hello readers”);). This is how you write text to a web page in the JavaScript. This is an example of using a JavaScript function (also known as method).


How to include external JavaScript in HTML file:

If we use a lot of scripts, or our scripts are complex then including the code inside the webpage will make the source file difficult to read and debug. A better idea is to put the JavaScript code in a separate file and include that code in the HTML file. By convention, JavaScript programs are store in a file with the .js extension. The general format is,

<html>
<head>
<script language=”javascript” src=”sample.js”>
</script>
</head>
<body>
………….
………..
…………
</body>
</html>

Thursday 11 June 2020

Uses of javascript | What is javascript used for - Online help

hello readers, in the last post we have discussed the javascript introduction briefly so now in this post, we'll see the uses of javascript?

Uses of javascript | What is javascript used for - Online help

Uses of JavaScript:

The JavaScript was initially introduced to provide programming capability at both the server and client ends of web connection.

JavaScript, therefore, is implemented at two ends:
  •  client end
  •  back end
The client-side JavaScript is embedded in XHTML documents and is interpreted by the web browser.

It also provides some means of computation, which serves as an alternative for some tasks done at the server-side.

Interaction with users through form elements, such as button and menus, can be conveniently described in JavaScript. Because button clicks and mouse movements are easily detected with JavaScript, they can be used to trigger computations and provide feedback to the user.

For example, when a user Moves the mouse cursor from the textbox, JavaScript can detect that moment and check the appropriateness of the textbox’s value(which presumably was just filled by the user).

Even without forms, user interaction is both possible and simple to the program in JavaScript. These interactions, which take place in the dialogue Windows, includes getting input from the user and allowing the user to make choices through the buttons. It is also easy to generate new content in the browser displays dynamically.

This transfer of tasks ensures that the server is not overloaded and performs only the required task.

But client-side JavaScript cannot replace servers side JavaScript; because server-side software supports file operation, database access, security, and networking, etc.

JavaScript is also used as an alternative to Java applets.

Programming in JavaScript is must simpler than compared to Java

JavaScript support DOM (document object model) which enables JavaScript to access and modify CSS properties and content of any element of a displayed XHTML document.

I hope you guys understand the uses of the javascript. If you have any queries or you find any mistake please comment down below. 

Wednesday 10 June 2020

What is JavaScript used for in web design - Online Help

What is JavaScript used for in web design:

Hello, readers in this post we will gonna see what is JavaScript used for in web design. let's get started.

What is JavaScript used for in web design - Online Help

The primary use of JavaScript is to write functions that are embedded in or included from HTML pages and interact with the document object model(DOM)of the page. Some simple examples of  this usage are:
  • Opening or popping up a new window with programmatic control over the size, position, and attributes of the new window(i.e. whether the menus, toolbar, etc are visible). 
  • Validation of web form input value to make sure that they will be accepted before they are submitted to the server.
  • Changing images as the mouse cursor moves over them: This effect is often used to draw the user's attention to important links displayed as graphical elements.
Because JavaScript code can run locally in the user's browser(rather than on the remote server) it can respond to user action quickly, making an application feel more responsive.

Furthermore, JavaScript code can detect user actions with HTML alone cannot, such as individual keystrokes.

An application such as Gmail takes advantage of this: much of the user interface logic is written in JavaScript, and Javascript dispatches requests for the information(such as the content of an email message) to the server.

So guys let's see what can a Javascript do.

Also read:  Multithreading in java

what can a JavaScript do:

JavaScript gives web developers a programming language for the use in the web pages and allows them to do the following:

JavaScript gives HTML designer a programming tool: 

HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple Syntax! almost anyone can put simple "snippets" of code into their HTML pages.


JavaScript can put dynamic text into HTML pages:   

A JavaScript statement like this: document.write("<h3>"+name+"</h3>") can write a variable text into HTML page.


JavaScript can react to events:

A JavaScript can be set to execute when something happens like when a page has finished loading or when a user clicks on the HTML element.


JavaScript can read and write HTML elements:

A JavaScript can read and change the content of the HTML element.

Also Read: Thread in java

JavaScript can be used to validate data:

A JavaScript can be used to validate form data before it is submitted to the server. This saves the server from extra processing.


JavaScript can be used to detect the visitor's Browser:

A JavaScript can be used to detect the visitor's browser and depending on the Browser-load another page specifically designed for that browser.


JavaScript can be used to create cookies:

A JavaScript can be used to store and retrieve information on the visitor's computer.

Also read: Exception handling in java

So guys here I had briefly explained what is JavaScript used for in web design. I hope you guys understand the uses of JavaScript in web design if you have any doubts please comment in the comment section below. Thank you for visiting and reading my site.

Posts you may like:


Tuesday 9 June 2020

Javascript introduction| what is Javascript| History of javascript -Online Help

Javascript Introduction:

The important topic of web technology is JavaScript. Now, what is JavaScript let's see the JavaScript introduction?


Javascript introduction| what is Javascript| History of javascript -Online Help


JavaScript is a client-side scripting language and connected to the server.

It is widely used in tasks ranging from validation of form data to the creation of complex user interfaces.

Javascript is a dynamic computer programming language and powerful client-side scripting language. It is mainly designed for enhancing the interaction of the user with the web pages.

JavaScript not only used in web pages but also used in many desktop and server programs as well as in-game development and mobile application development.

Dynamic HTML is a combination of the content formatted using HTML, CSS, scripting language, and DOM by combining all of these technologies we can create an interesting and interactive website.

History of JS:

Netscape initially introduced the language under the name LiveScript in an early beta release of navigator 2.0 in 1995, and the focus was on form validation. After that, the language was renamed JavaScript.

After Netscape introduced JavaScript in version 2.0 of their browser, Microsoft introduced a clone of JavaScript call JS script in internet explorer 3.0.

Also read: Java string | string classes in java

What is JavaScript?

  • JavaScript was designed to add interactivity to the HTML pages.
  • JavaScript is a scripting language.
  • It is the lightweight programming language.
  • JavaScript is usually embedded directly into HTML pages.
  • JavaScript is an interpreted language that means which executes without preliminary completion.
  • Everyone can use JavaScript without purchasing a license.

Are Java and JavaScript the same?

No, Java and JavaScript are not the same. In fact, they are completely different languages in both concepts and designs.

Java {developed by Sun Microsystem} is a powerful and much more Complex program - in the same category as C and C++.

Let's see the comparison between Java and Javascript:

Java

JavaScript

  • Java is a programming language
  • JavaScript is a scripting language
  • It is strongly typed language
  • It is dynamically typed language
  • Types on own at the compile time
  • Compile-time type checking is impossible
  • The objects in Java is static
  • Javascript Object are dynamic
  • Collection of data member and the method is fixed at the compilation time
  • The number of data member and methods of an object can change during execution
  • Object-oriented programming language
  • Object-based language

I hope you guys understand what is JavaScript and the introduction briefly. Let's see the uses and advantages of it in the next post if you have any queries please comment down below. Thank you for visiting my site.

Monday 9 March 2020

Java String | String class in java | java string methods - Online Help

In this post, we are discussing what is java string? that is what is a string class in java.let's see the string constructors and methods available in java. The topic which we will be going to cover in this post is

Java String | String class in java - Online Help

String class in Java:

A combination of characters is a string. Strings are instances of the class string. They are real objects and enable combination, testing, and modification. When a string literal is used in the program, Java automatically creates an instance of the String class. 

The string is a sequence of characters enclosed within double quotes “ “. This class is used to represent the constant string.it is fixed size. These classes are contained in the package java.lang that represents a string in Java that provides two fundamental classes that represent a string in Java. They are:


String declaration in Java:

Before we use a string in our program we have to declare it. so the java string Syntax is,

String stringname;

Where,

  • String is the java literal
  • Stringname is the name of the string

Example:
String s;

String initialization in Java:

The string can be initialized at the time of the string declaration. The value which we assign to the string should be enclosed with the double quotes " ''. so the initialization of the string is,

String strname=” variables”;

Example:

String s=” Sania”;

We can use an array in the declaration and initialization of string. let’s pass the array of values to the string,

String name[]={“Rohit”,” Rahul”,” Rohan”,” pooja”,” Sneha”};

Accessing the string:

In order to access the string, we have to create an instance for the string class. With the help of instance, we can access the string.

In the next section, let's see how to create Constructors and what are the different ways available in constructors so that we can understand the topics on how to create and access the instance.

Drawbacks of string class:


  • String class objects are immutable because once it is created the content of the string cannot be modified.
  • Because it is immutable it needs more memory space.

String program in Java:

Declaration and access of strings
Class strings
{
int i;
String name[]={“aaaa”,”bbbb”,”cccc”,”dddd”};
Void show()
{
System.out.println(“my favourite names are”);
for(i=0;i<5;i++)
System.out.println(name[i]);
}
}
Public static void main(String args[])
{
Strings s=new strings();
s.show();
}
}

The output is:

aaaa
bbbb
cccc
dddd

String constructors in Java:

Different types of String constructors are available in Java so that you can use the string in different ways as per your requirement. With these constructors, you will go through with different types of string declaration and initialization. The different types of the constructor are:

Empty string:

Let's see how empty string is created using the class string. It is the default constructor with no parameter.

The general form is,

String stringname;
Stringname=new String();

In the above statement is equivalent to the following statement:

String stringname=new string();

Example:

String str1;
str1=new String();

Is equivalent to

String str1=new String();

Or

String str=” ”; // empty string

The example will create an instance of a string with no characters in it.

String with character:

In order to create a string initialized with the character, we need to pass an array of characters to the constructor. let's see how the string is created with the character.

The general format is,

String strname=new String(character);

where the string and new are keywords. Strname is the name of the string. The String(character) is the constructor character in which the character is to assign.

Example:
Char chars[]={‘z’,’u’,’h’,’a’};
String s=new String(name);
System.out.println(s);

The above example creates a string instance with the four characters from the char as the initial value of the string s.

String with another string:

let's see how a string is created with another string. The general format to create a string with another string is,

String strname1=new String(strname2);

Where,

  • The string and new are keywords
  • The strname1,2 is the name of the string. 
  • In the string(), strname1 is assigned to the strname2

Example:
String str1=”sneha”;
String str2=new String(str1);
System.out.println(str2);

String with substring:

A string is created using the substring that allowed the specification of the starting index and the number of characters that have to be used. The general format is,

String strname=new String(char chars[],int startIndex, int numberofchar);

Where,

  • The String and new are keywords
  • char chars[] is the original string of character
  • startIndex is the starting index of the substring
  • Numberofchar is the ending number of substring.

Example:
char name[]={‘s’,’n’,’e’,’h’,’a’}
String str=new String(name,1,4);
System.out.println(str);

The example given above will print neha because when ‘n’ was at the index at 1 and we specified the count of 4 characters to be used to construct the string.

String with an array of ASCII:

let's see how a string is created with the array of ASCII parameters. The general format is,

String strname=new String(ASCIInum);

Where,

  • String and new are keywords. 
  • strname is the name of the string 
  • ASCII num is the ASCII value.

Example:

int value[]={77,65,78};
String str1=new String(value);
System.out.println(str1);

it will return the output as MAN.

string methods in Java:

The string class contains a number of methods that allow you to perform string manipulation. Most commonly used string methods are as follow:

Methods
Description
General format
length()
Number of character in string
Stringobject.length()
charAt()
The char at a location in the string
Stringobject.charAt(int indexvalue);
getChars(),getBytes()
Copy chars or bytes into a external array
Stringobject.getChars();
toCharArray()
Produces a char[]contains the character in the string
Stringobject.toCharArray()
equals(),equalsIgnoreCase()
An equality check on the content of the two strings
Stringobject.equals(string);
compareTo()
Results is negative,0 or + depending on the lexicographical ordering of the string and the argument. Uppercase and the lower case are not equal.
Stringobject.compareTo(string1);
regionMatches()
Boolean result indicates whether the region matches
 boolean regionMatches()
startWith()
Boolean result indicating whether the region matches
 boolean startWith()
endsWith()
The boolean result indicates if the argument is a suffix
boolean endsWith()
indexOf(),lastIndexOf()
Return -1 if the argument is not found within the string,otherwise returns the index where the arguments start. lastIndexOf() searches backward from the end.
Stringobject.indexOf(char);
Stringobject.lastIndexOf(char);
Substring()
Return a new string object contains the specified character set.
1.   Stringobject.substring(int beginindex);
2.   Stringobject.substring(int indexbegin,int endindex);
Concat()
Return a new string object contain the original strings character followed by the characters in the argument.
Stringobject.concat(string);
Replace()
Return a new string object with the replacements made.uses the old string if no matches found.
Stringobject.replace(oldstring,newstring);
toLowerCase(),toUpperCase()
Return a new string object with the case of all letter changed.uses the old string if no changes need to be made.
Stringobject.toLowerCase();
Stringobject.toUpperCase();
Trim()
Return the new string object with the whitespace removed from each end.uses the old string if no changes need to be made.
Stringobject.trim();
valueOf()
It returns a string containing a character representation of the argument.
String valueOf();
Intern()
Produces one and only string handle for each unique character sequence.
 String intern()


It is seen that every string method returns a new string object when it is necessary to change the contents. Also noticed that if the content does not require any change, the method will just return a handle to the original string. This saves storage and overhead.

Java String method example:

equal():

Class equaldemo
{
Public static void main(String args[])
{
String s1=”welcome”;
String s2=”welcome”;
String s3=”good morning”;
String s4=”welcome”
System.out.println(s1+”equals”+s2+”is”+s1.equals(s2));
System.out.println(s1+”equals”+s3+”is”+s1.equals(s3));
System.out.println(s1+”equals”+s4+”is”+s1.equals(s4));
}
}

The output is:

Welcome equals welcome is true
Welcome equals good morning is false
Welcome equals welcome is false

Sample program for :
length(),toLowerCase(),toUpperCase(),charAt(),concat(),indexOf(),lastIndexOf(),substring(),replace(),and trim()


Class st
{
Public static void main(String args[])
{
String s=”johny johny yes papa”+”eating sugar no papa”+”telling lies no papa”;
String s1=”hello world”;
String s2=”hello”;
String s3=”HELLO”;
String s4=”monkey”;
String s5=”don”
System.out.println(“index of e=”+s.indexOf(‘e’));
System.out.println(“last index of e=”+s.lastIndexOf(‘e’));
System.out.println(s1.substring(6));
System.out.println(s1.length());
System.out.println(s1.substring(3,8));
System.out.println(s2.concat(“world”));
System.out.println(s2.replace(‘l’,’w’));
System.out.println(s2.toUpperCase());
System.out.println(s3.toLowerCase());
System.out.println(s3.charAt(3));
System.out.println(s1.trim());
System.out.println(s4.compareTo(s5);
}
}

The output will be

johny johny yes papa eating sugar no papa telling lies no papa
index of e:14
11
last index of e: 62
world
lo wo
helloworld
hewwo
HELLO
Hello
l
hello world
6

String arithmetic:

The ‘+’ sign doesn't mean “addition” when it is used with the string object. The Java string class has something called operator overloading”, in another word the ‘+’ sign, when used with the string object, behave differently that does with everything else.

For string, it means: “concatenate these two strings”. when it is used with the strings and other objects creates a single string that contains the concatenation of all its operands.

When a numerical value is added to a string, the compiler calls the method that turns the numerical value( int, float, etc) into a string which can then be added with the Plus sign.

Any object or type you can convert to the string if the method to string() is implemented. To create a string, add all the parts together and output them. The +=operator will also work for the strings.

Example:
class sample
{
String fname=”rohit”;
String lname=”sharma”;
void show()
{
System.out.println(“The full name is+” “+fname+” “+lname);
}
Public static void main(String args[])
{
sample s1=new sample();
s1.show();
}
}

The output is;

The full name is rohit sharma

I hope you find this article useful. if you have any queries regarding this or something went wrong. Then please do comment.

Recommended post(you may like):