Publish your story - poem - jokes free online--click here

JAVA

Java Data Types Array String and its Functions

JAVA have eight fundamental data types divided in four groups:--

Group                                                                                   Data types
Integer                                                                                 byte(8 bits), short(16 bits), int(32bits), long(64 bits)
Floating Point                                                                    float(32 bits), double(64 bits)
Character                                                                            char(16 bits)
Boolean                                                                               boolean(compiler deprndent)

Variable Declaration in Java:--
Syntax :-- 
                                Data_type   variable_name;
                e.g.        int a;         char c;                   float f;

Taking input in java:--

To take input We have to make a object of a Scanner class (Defined  in java.util  package)

import java.util.*; //importing util package to use scanner class
public class abc
{
      public static void main(String as[])
      {
            Scanner in=new Scanner(System.in); //object of scanner class //system.in is for system input
            byte b;
            short s;
            int i;
            long l;
            float f;
            double d;
           
            //taking input using object of scanner class and its function
           
            b=in.nextByte();
            s=in.nextShort();
            i=in.nextInt();
            l=in.nextLong();
            f=in.nextFloat();
            d=in.nextDouble();
            System.out.println("byte value=="+b);
            System.out.println("short value=="+s);
            System.out.println("int value=="+i);
            System.out.println("long value=="+l);
            System.out.println("float value=="+f);
            System.out.println("double value=="+d);
      }
}


Array and String
Declaring Array in java:--
int ar[]=new ar[5];

input in array:--
for(int i=0;i<5;i++)
{
      ar[i]=in.nextInt();//in is object of scanner class
}

Declaring a string in Java:--
String s=new String(); //here String is a class and s is object //of string class
Input in String:--
s=in.nextLine();

coping a string in to other string:--
String s1=new String(“hello”);
String s2=new String(s1);//hello is copied to s2
String s3=new String();
S3=s2;//again hello is copied to s3;

Passing a character array in string:--
char ch[]={'a','b','c','d'};
String ss=new String(ch);//abcd is copied to ss


String functions

import java.util.*; //importing util package to use scanner class
public class abc
{
      public static void main(String as[])
      {
            Scanner in=new Scanner(System.in); //object of scanner class 
//system.in is for system input
            String s1=new String();
            String s2=new String();
            System.out.println("enter any string");
            s1=in.nextLine();
           
            //finding length of string s1
            int l=s1.length();
            System.out.println("length of string=="+l);
           
            //taking a character from any index say 2  index is always less by
 //one to position
            char c=s1.charAt(2);
            System.out.println("character at index two=="+c);
           
            //checking  equality of two string
            s2="abc";
           
            if(s2.equals("Abc"))//equal() returns false because abc is not equal 
//to Abc (A is upper case)
            {
                  System.out.println("string is not equal");
            }
           
            if(s2.equalsIgnoreCase("Abc"))//equalIgnorecase() returns
 //ture because it ignore case
            {
                  System.out.println("strings are equal");
            }
           
            //comparing two string
            l=s2.compareTo(s1);//value of l will be 0 if s1==s2  other wise non
 //zero value
            l=s2.compareToIgnoreCase(s1);//ignore the case
           
            //Adding one string at end of other string
            s1=s1.concat(s2);//s2 will be added at the end of s1
            System.out.println(s1);
           
            //convert string in upper case
            s1=s1.toUpperCase();
            System.out.println(s1);
           
            //convert string in lower case
            s1=s1.toLowerCase();
            System.out.println(s1);
      }
}

No comments:

Post a Comment