on Leave a Comment

Introduction to Javascript

Javascript is a client side scripting language. It is a primary used language on the web. It is lightweight and interpreted language. Javascript has ability to change HTML content. Today all modern web browsers support javascript. 

Javascript is developed by Netscape Communication at 1995. Javascript was first known as liveScript and then it changed to Javascript, may be because of popularity of Java. 

Advantage of Javascript

It is client side scripting language. Javascript code runs by the user processer and thus it saves bandwidth interaction with server.

Javascript is easy to learn and it’s much look like English language. 

Javascript has plenty of predefined function, variable thus provides greater functionality to the user.

Using javascript you can create interactive web pages that catches attraction of site visitor.

It is fast because it runs on user system and its need not be process on web server.

Limitations of Javascript

Javascript code can be used by hackers to infect user's system, so it is security issue. Modern web browsers set some restriction but still, it’s not fully secure.

Using javascript, you can't work with files on server because it is a client side scripting language.

Javascript doesn't support multithreading.

Javscript depends on browser, so it is interpreted differently on different browsers.

All browsers has option to disable javascript, so user can disable javascript. 



on Leave a Comment

Java Program to Find Duplicate Characters in a String

This java program finds duplicate characters in a string. In this program, we assign a string to a variable named "str" and this string is converted to character array using toCharArray() method and assigned to a variable "c". Then using nested for loop, we compare each character of char array to the other characters in array and if they match, we print that character.

Java Program to Find Duplicate Characters in a String

class FindDuplicateString{
 public static void main(String[] args){
  String str = "java";
  char[] c = str.toCharArray();
  System.out.print("Duplicate characters : ");
  for(int i=0; i<str.length(); i++){
   for(int j=i+1; j<str.length(); j++){
    if(c[i] == c[j]){
              System.out.print(c[i]+" ");         }
   }   
  }
 }
}

Output:

Duplicate characters : a



on Leave a Comment

Java Program to Convert String to Int

In java, we can convert string to integer using static methods like parseInt() and valueOf(). 

Java String to Int Using Integer.parseInt()

parseInt() is a static method of Integer class, this method returns a primitive int.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  int a = Integer.parseInt(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110

Java String to Int Using Integer.valueOf()

valueOf() method returns a Integer object.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  Integer a = Integer.valueOf(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110




on Leave a Comment

Java Program to Compare Two Strings

In java, we can compare two strings using some predefined methods namely, equals() and equalsIgnoreCase() method. Strings can also be compared using == operator.

Java program to compare strings by equals() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equals(s2));
   
      //compare s1 with s3
      System.out.println(s1.equals(s3));
   
      //compare s1 with s4
      System.out.println(s1.equals(s4));
   }
}

Output:

true
false
true

equals() method return true if sequence of characters is same in both string objects, otherwise it returns false.

Java program to compare strings by equalsIgnoreCase() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equalsIgnoreCase(s2));
   
      //compare s1 with s3
      System.out.println(s1.equalsIgnoreCase(s3));
   
      //compare s1 with s4
      System.out.println(s1.equalsIgnoreCase(s4));
   }
}

Output:

true
true
true

equalsIgnoreCase() method ignore the case and returns true if both string objects have same sequence of character, otherwise it returns false.






on Leave a Comment

Java Program to Find ASCII value of a Character

ASCII(American Standard for Information Interchange) is a code to represent English characters using numeric code. It includes upper and lower case English letters, numbers and punctuation symbols. It assigns a number to each character from 0 to 127. For example ASCII code for alphabet A is 65. In Java, we can find ASCII value of character by assigning a character value to integer variable.

Java Program to Find ASCII Value of Character

public class ShowAscii {

    public static void main(String[] args) {

        char ch = 'A';
        int asciiCode = ch;
        // we can also cast char to int
        int castascii = (int)ch;

        System.out.println("ASCII value of "+ch+" is: " + asciiCode);
        System.out.println("ASCII value of "+ch+" is: " + castascii);
    }
}

Output:

ASCII value of A is: 65
ASCII value of A is: 65