JavaScript: parseInt() Function

Posted on Updated on

  • The parseInt() function parses a string and returns an integer.

Syntax: parseInt(string, radix)

String is required and radix is optional.

  • The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.If the radix parameter is omitted, JavaScript assumes the following:
    • If the string begins with “0x”, the radix is 16 (hexadecimal)
    • If the string begins with “0”, the radix is 8 (octal). This feature is deprecated
    • If the string begins with any other value, the radix is 10 (decimal)
    • Only the first number in the string is returned!
    • Leading and trailing spaces are allowed.
    • If the first character cannot be converted to a number, parseInt() returns NaN.

     

Example:

var a = parseInt(“10”) + “<br>”;—-10
var b = parseInt(“10.00”) + “<br>”;—-10
var c = parseInt(“10.33”) + “<br>”;—-10
var d = parseInt(“34 45 66”) + “<br>”;—34
var e = parseInt(” 60 “) + “<br>”;—–60
var f = parseInt(“40 years”) + “<br>”;—-40
var g = parseInt(“He was 40”) + “<br>”;—NaN

var h = parseInt(“10”, 10)+ “<br>”;—-10
var i = parseInt(“010”)+ “<br>”;—–10
var j = parseInt(“10”, 8)+ “<br>”;—-8
var k = parseInt(“0x10”)+ “<br>”;—-16
var l = parseInt(“10”, 16)+ “<br>”;—–16

Advertisements

Java:String Pooling

Posted on Updated on

  • String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is special class in java.
  • String pooling concept can be better understood with the below diagram.

String-Pool-Java1

  • Hence when u do not use the new keyword the object is referenced to the same memory location.

Java: Difference between path & Classpath

Posted on Updated on

Difference between path and classpath

path classpath
  • path variable is used to set path for all java software tools like javac.exe, java.exe, javadoc.exe, and so on.
  • Path is an environment variable which is used by the operating system to find the executables i.e setting up environment for operating system
  • Classpath is an environment variable which is used by the Java compiler to find the path, of classes.ie in J2EE we give the path of jar files.
  • classpath variable is used to set path for java classes which are used in our application.

difference between path and classpath

Java: finalize() method

Posted on

  • The java.lang.Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
  • This method is defined in Object class as:  protected void finalize(){}
  • If an object is created without keyword new we can use finalize method to perform cleanup processing.

Java: toString() method

Posted on

  • toString() method is used to retrieve object information in string format.
  • If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Let us understand with an example:

class Student{  

 int rollno;  

 String name;  

 String city; 

 Student(int rollno, String name, String city){  

 this.rollno=rollno;  

 this.name=name;  

 this.city=city;  

 }  

 public static void main(String args[]){  

   Student s1=new Student(101,“Raj”,“lucknow”);  

   Student s2=new Student(102,“Vijay”,“ghaziabad”);  

   System.out.println(s1);//compiler writes here s1.toString()  

   System.out.println(s2);//compiler writes here s2.toString()  

 }  

}  

Output:

Student@1fee6fc
Student@1eed786

As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let’s understand it with the example given below:

class Student{  

 int rollno;  

 String name;  

 String city;  

  Student(int rollno, String name, String city){  

 this.rollno=rollno;  

 this.name=name;  

 this.city=city;  

 }  

   public String toString(){//overriding the toString() method  

  return rollno+” “+name+” “+city;  

 }  

 public static void main(String args[]){  

   Student s1=new Student(101,“Raj”,“lucknow”);  

   Student s2=new Student(102,“Vijay”,“ghaziabad”);  

   System.out.println(s1);//compiler writes here s1.toString()  

   System.out.println(s2);//compiler writes here s2.toString()  

 }  

}

Output:
101 Raj lucknow
102 Vijay ghaziabad

Java : Does a Try block be followed by catch block

Posted on

  • Each Try block must be followed by either catch or finally.
  • Let us see what happens when you use try block alone

public class Testtry{
public static void main(String args[]){
try{
System.out.println(“code…”);
}
}
}

Output:

error: ‘try’ without ‘catch’, ‘finally’ or resource declarations
try{
^
1 error

  • Thus try must be followed by either catch or finally.

 

exceptionobject

 

 

Java:Can Finally block be used without Catch.

Posted on

The answer is YES.

  • The finally block always executes when the try block exits. So you can use finally without catch but you must use try.

Let us understand this with an example

class TestFinallyBlockWithoutCatch{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}

finally{

System.out.println(“finally block is always executed”);

}
System.out.println(“rest of the code…”);
}
}

Output:

Exception in thread “main” java.lang.ArithmeticException: / by zero
at TestFinallyBlock1.main(TestFinallyBlock1.java:4)

finally block is always executed
finally