Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.
class ConsInfo {
 public static void main(String[] args) {
  if (args.length == 0) {
   System.out.println("PLEASE PASS THE CLASS NAME..!");
  } else {
   try {
    Class c = Class.forName(args[0]);
    printConsts(c);
   } catch (ClassNotFoundException cnfe) {
    System.out.println(args[0] + " DOES NOT EXISTS");
   }
  }
 }
 static void printConsts(Class c) {
  java.lang.reflect.Constructor Cons[] = c.getConstructors();
  System.out.println("NUMBER OF CONSTRUCTORS:" + Cons.length);
  System.out.println("NAME OF THE CONSTRUCTOR:"+c.getName());
  for (int i = 0; i < Cons.length; i++) {
   System.out.print(c.getName() + "(");
   Class cp[] = Cons[i].getParameterTypes();
   for (int j = 0; j < cp.length; j++) {
    System.out.print(cp[j].getName() + ")");
   }
   System.out.println("\b" + ")");
  }
 }
}
A name in the program is an identifier it may be class name or method name, variable name or label name.


Rules for defining Identifiers

1) A java identifier is a sequence of characters, where each character may be a letter from a-z or A-Z or a digit form 0-9 or currency symbol $ or connecting punctuation – , if we are using any other symbol we will get Compile time error “IllegalCharacter”.

2) Identifier should not be starts with digit.

3) There is no length limit for java identifiers but it is not recommended to take more than 15
length.

4) Java Identifiers are case sensitive

Example:

class FundaDemo 
{
    public static void main(String[] args)
    {
        int String = 10;
        System.out.println(String);
    }
}

Output- 10


Exercise- Which of the following are valid Identifiers

1) total#
2) all@hands
3) 123total
4) break
5) String
6) total_number
7) $_$
8) $ca$h

Ans- 5,6,7,8

import java.util.Scanner;
public class Series 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  double sum = 0;
  for (int i = 1; i <= n; i++)
   sum = sum + (double) 1 / Math.pow(i, 2);
  System.out.println("Sum of series = " + sum);
 }
}

Output:
Enter the no of terms 
5
Sum of series = 1.4636111111111112
BUILD SUCCESSFUL (total time: 2 seconds)


import java.util.Scanner;
public class Series  
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  int sum = 0, i = 0;
  for (int j = 1; j <= n; j++) 
  {
   for (i = 1; i <= j; i++) 
   {
    sum = sum + i;
   }
   i = 1;
  }
  System.out.println("Sum of series = " + sum);
 }
}

Output:
Enter the no of terms 
4
Sum of series = 20
BUILD SUCCESSFUL (total time: 2 seconds)

import java.util.Scanner;
public class  Series 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  double sum = 0;
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  for (int i = 1; i <= n; i++) 
  {
   if (i % 2 == 0) {
    sum = sum - (double) 1 / (i * i * i);
   } else {
    sum = sum + (double) 1 / (i * i * i);
   }
  }
  System.out.println(" Sum of the series : " + sum);
 }
}

Output:

Enter the no of terms 
5
Sum of the series : 0.904412037037037
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class Series  
{
 public static void main(String args[])
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                                          // s for terms of series, c for n terms
  for (c = 1; c <= n; c++)                          // To generate n terms
  {
   s = s * 10 + 1;
   System.out.print(s + " ");
  }                                                           //for  ends
 }
}


Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111...... 
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                       // s for terms of series, c for counter to generate n terms
  for (c = 1; c <= n; c++) {
   s = s * 10 + c;
   System.out.print(s + " ");
  }
 }
}


Output:
Enter the number of terms: 6
1,  12,  123,  1234,  12345,  123456....... 
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class OddSeries 
{
 public static void main(String args[]) 
  {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int i = 1, c, f = 1;                            // i for odd nos, c for counter, f for flag
  for (c = 1; c <= n; c++) {
   if (f % 2 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
   i += 2;
   f++;
  }                                                  //Loop ends
 }
}


Output:


Enter the number of terms: 6

1  -3  5  -7  9  -11...... 
BUILD SUCCESSFUL (total time: 6 seconds)

import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  int c, i = 2, n;                                          // c for counter, i for even nos.
  System.out.print("Enter the number of terms: ");
  n = sc.nextInt();
  System.out.print("\n");
  for (c = 1; c <= n; c++, i += 2)               //to generate n terms of the series
  {
   if (i % 4 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
  }
 }
}

Output:

Enter the number of terms: 10
2 -4 6 -8 10 -12 14 -16 18 -20 
BUILD SUCCESSFUL (total time: 6 seconds)