Thursday, August 16, 2018

Scanner

Example

import java.util.Scanner;
class GetInputs {
public static void main(String args[]) {
int a; float b; String s;
Scanner obj = new Scanner(System.in); /* create a object */ System.out.println("Enter a string:");
s = obj.nextLine(); /* Take string input and assign to variable */ System.out.println("You entered string "+s); /* Print */
System.out.println("Enter an integer:");
a = obj.nextInt(); /* Take integer input and assign to variable */ System.out.println("You entered integer "+a); /* Print */
System.out.println("Enter a float:");
b = obj.nextFloat(); /* Take float input and assign to variable */ System.out.println("You entered float "+b); /* Print */ } }

Program Output:

Enter a string: w3schools
You entered string w3schools
Enter an integer: 15
You entered integer 15
Enter a float: 12.5
You entered float 12.5

Tuesday, August 7, 2018

Import

import java.util.*;

import java.util.Scanner; 

In java -
util :stands for utility and contains utility classes. 
Scanner : is a predefined class for taking inputs from user.

Monday, August 6, 2018

Java overloading vs overriding

Definitions

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

2. Overriding vs. Overloading

Here are some important facts about Overriding and Overloading:

1). The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime.
In contrast, reference type determines which overloaded method will be used at compile time.

2). Polymorphism applies to overriding, not to overloading.

3). Overriding is a run-time concept while overloading is a compile-time concept.

3. An Example of Overriding

Here is an example of overriding. After reading the code, guess the output.

class Dog{
public void bark(){
System.out.println("woof ");
} }
class Hound extends Dog{
public void sniff(){
System.out.println("sniff ");
}  
public void bark(){
System.out.println("bowl");
} }  
public class OverridingTest{
public static void main(String [] args){
Dog dog = new Hound(); dog.bark();
} }

Output:

bowl

In the example above, the dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned to dog. The JVM knows that dog is referring to the object of Hound, so it calls the bark()method of Hound. This is called Dynamic Polymorphism.

4. An Example of Overloading

class Dog{
public void bark(){ System.out.println("woof ");
}   //overloading method
public void bark(int num){
for(int i=0; i<num; i++) System.out.println("woof ");
} }

In this overloading example, the two barkmethod can be invoked by using different parameters. Compiler know they are different because they have different method signature (method name and method parameter list).

Java intro

What is Java technology and why do I need it?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

Is Java free to download?

Yes, Java is free to download. Get the latest version at java.com.

If you are building an embedded or consumer device and would like to include Java, please contact Oracle for more information on including Java in your device.


Notice that Java is just a name not an acronym.

Originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995.

In 1995, Time magazine called Java one of the Ten Best Products of 1995.

JDK 1.0 released in(January 23, 1996).

Java Version History

There are many java versions that has been released. Current stable release of Java is Java SE 10.

JDK Alpha and Beta (1995)


JDK 1.0 (23rd Jan, 1996)


JDK 1.1 (19th Feb, 1997)


J2SE 1.2 (8th Dec, 1998)


J2SE 1.3 (8th May, 2000)


J2SE 1.4 (6th Feb, 2002)


J2SE 5.0 (30th Sep, 2004)


Java SE 6 (11th Dec, 2006)


Java SE 7 (28th July, 2011)


Java SE 8 (18th March, 2014)


Java SE 9 (21st Sep, 2017)


Java SE 10 (20th March, 2018)


Tuesday, July 31, 2018

Find the string length

Syntax

Stringname.length()


import java.io.*;

public class Test

{
public static void main(String args[])
{
String Str1 = new String("Welcome to      Tutorialspoint.com");

String Str2 = new String("Tutorials" );

System.out.print("String Length :" );

System.out.println(Str1.length());

System.out.print("String Length :" );

System.out.println(Str2.length());

} }