Overview of Computers

Two systems of computers:
1) PC – the Personal Computer              
2) Mac – the Apple Macintosh.

Two designs of computers:
1) desktop
2) laptop (notebook)

Computer architecture:  
Main components of a computer            Multimedia devices                  Other peripheral devices

1)  computer                                        1)  CD-ROM / DVD drive         1)  printer            
2)  monitor                                          2)  video card                              2)  scanner  
3)  hard disk/ hard drive                      3)  soundcard                              3) CD-recorder,CD-R/CD- RW  4)  keyboard                                        4)  speakers                                4)  modem      
5)  mouse / trackball /                         5)  headphones / headset             5)  USB flash drive                
    touch pad                                        6)  microphone                             6)  webcam            
                                                                                                                 7)  digital camera
                                                                                                                 8)  digital voice recorder
                                                                                                                 9)  camcorder
Inside the Computer

1) Processor:
The CPU (Central Processing Unit), a complete computation engine that is fabricated on a single chip, is the computer’s brain. It is sometimes referred to as the central processor, microprocessor, or just processor. Two typical components of a CPU are: 1) the arithmetic logic unit (ALU), which performs arithmetic and logical operations, and 2) the control unit, which extracts instructions from memory and decodes and executes them, calling on the ALU when necessary.

2) Memory:
ROM (Read Only Memory) is the computer’s permanent, long-term memory. Tere are types of ROM called PROM that can be altered. The P stands for programmable. ROM's purpose is to store the basic input/output system (BIOS) that controls the start-up, or boot process.
RAM (Random Access Memory) is a working area where the operating system (e.g. Windows), programs and data in current use are kept, ready to be accessed by the processor. It is the best known form of computer memory. However, RAM, unlike ROM, is emptied when the computer is switched off. The more RAM you have, the quicker and more powerful your computer is.
There are two basic types of RAM: dynamic RAM (DRAM) and static RAM (SRAM).
The two types differ in the technology they use to hold data. DRAM, the more common type, needs to be refreshed thousands of times per second. SRAM does not need to be refreshed, which makes it faster, but it is also more expensive than DRAM.

Memory is measure in the following units:
-  1 byte = 8 bits (Each 1 or 0 is called a bit (i.e. binary digit). Each character (i.e. a letter, a number, a space, or a punctuation mark) has its own arrangements of 8 bits, e.g. 01000001 = “A”, 01000010 = “B”.
-  1 KB (kilobyte) = 1024 (210) bytes
-  1 MB (megabyte) = 1024 (210) KB
-  1 GB (gigabyte) = 1024 (210) MB
     
Cache (pronounced as "cash") is a buffer (made of a small number of very fast memory chips) between main memory and the processor. It temporarily stores recently accessed or frequently-used data. Whenever the processor needs to read data, it looks in this cache area first. If it finds the data in the cache, then the processor does not need to do more time-consuming reading of data from the main memory. Memory caching allows data to be accessed more quickly.

Storage Devices
The most common forms of storage devices in a home computer are:
- Hard disk drive
- Floppy disk
- CD-ROM
- CD-R and CD-RW
- DVD-ROM
- USB flash drives

Monitor
There are two types of monitors available for PCs: the traditional CRT (cathode ray tube) and the newer LCD (liquid crystal display). The CRT is used for both televisions and computers. It produces a good quality image at a number of different settings for a reasonable price. LCD monitors, also known as flat panel displays, are used in laptop (or notebook) computers and more frequently for desktops as well. They are lighter and smaller (only inches thick) than CRTs with reduced electromagnetic emissions and power consumption.

Video Card
The card here is a jargon for an electronic circuit board. Video cards are also known as graphics cards, which are responsible for displaying 2D and 3D images on your monitor. 2D graphics are the regular pictures and images that appear on your screen while 3D graphics are mostly used in games and imaging.

Sound Card
A sound card is an electronic circuit board that is mounted inside the computer to control sound output to speakers or headphones, to record sound input from a microphone connected to the computer, and to manipulate sound stored on a disk. Sound cards are essential for multimedia applications and have become common on modern personal computers.
Many sound cards are also Windows-compatible. Many multimedia applications require the system to have a Windows-compatible sound card to run properly.

Ports
A port is an interface on a computer to which you can connect a device. Personal computers have various types of ports. Internally, there are several ports for connecting disk drives, monitors, and keyboards. Externally, personal computers have ports for connecting modems, printers, mice, and other peripheral devices.

There are three common types of external ports that usually come with a computer:

1) Parallel ports (for most printers)
2) Serial ports (for most modems and some mice)
3) USB (Universal Serial Bus) ports (for about every peripheral made in a USB version)

Java - Basic Syntax

When we consider a Java program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods, and instance variables mean.

Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class.

Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.

Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.

Instance Variables − Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

Java - Overview

Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]).

With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms.

The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere.

Java is 

Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.

Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.

Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.

Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.

Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.

Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.

Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.

High Performance − With the use of Just-In-Time compilers, Java enables high performance.

Distributed − Java is designed for the distributed environment of the internet.

Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

History of Java

James Gosling initiated Java language project in June 1991 for use in one of his many set-top box projects. The language, initially called ‘Oak’ after an oak tree that stood outside Gosling's office, also went by the name ‘Green’ and ended up later being renamed as Java, from a list of random words.

Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms.

On 13 November, 2006, Sun released much of Java as free and open source software under the terms of the GNU General Public License (GPL).

On 8 May, 2007, Sun finished the process, making all of Java's core code free and open-source, aside from a small portion of code to which Sun did not hold the copyright.

Java String - Programming Examples

1. How to compare two strings in Java ?

2. How to search the last position of a substring ?

3. How to remove a particular character from a string ?

4. How to replace a substring inside a string by another one ?

5. How to reverse a String?

6. How to search a word inside a string ?

7. How to convert a string totally into upper case?

8. How to optimize string concatenation ?

How to optimize string concatenation ?

Following example shows performance of concatenation by using "+" operator and StringBuffer.append() method.

public class StringConcatenate {
   public static void main(String[] args) {
      long startTime = System.currentTimeMillis();
     
      for(int i = 0;i<5000 i="" p="">         String result = "This is"
            + "testing the"
            + "difference"+ "between"
            + "String"+ "and"+ "StringBuffer";
      }
      long endTime = System.currentTimeMillis();
      System.out.println("Time taken for string"
         + "concatenation using + operator : "
         + (endTime - startTime)+ " ms");
      long startTime1 = System.currentTimeMillis();
     
      for(int i = 0;i<5000 i="" p="">         StringBuffer result = new StringBuffer();
         result.append("This is");
         result.append("testing the");
         result.append("difference");
         result.append("between");
         result.append("String");
         result.append("and");
         result.append("StringBuffer");
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("Time taken for String concatenation"
         + "using StringBuffer : "
         + (endTime1 - startTime1)+ " ms");
   }
}

Result
The above code sample will produce the following result. The result may vary.

Time taken for stringconcatenation using + operator : 0 ms
Time taken for String concatenationusing StringBuffer : 22 ms

-------------------------------------------------------------------------------------------------------------

How to do string concatenation ?

Following example shows concatenation string. This method returns a String with the value of the String passed into the method, appended to the end of the String, used to invoke this method.

public class HelloWorld {
   public static void main(String []args) {
      String s = "Hello";
      s = s.concat("word");
      System.out.print(s);
   }
}

Result
The above code sample will produce the following result. The result may vary.

Helloword

How to convert a string totally into upper case?

Following example changes the case of a string to upper case by using String toUpperCase() method.

public class StringToUpperCaseEmp {
   public static void main(String[] args) {
      String str = "string abc touppercase ";
      String strUpper = str.toUpperCase();
      System.out.println("Original String: " + str);
      System.out.println("String changed to upper case: " + strUpper);
   }
}

Result
The above code sample will produce the following result.

Original String: string abc touppercase 
String changed to upper case: STRING ABC TOUPPERCASE 

How to search a word inside a string ?

This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found. Otherwise it returns -1.

public class SearchStringEmp{
   public static void main(String[] args) {
      String strOrig = "Hello readers";
      int intIndex = strOrig.indexOf("Hello");
     
      if(intIndex == - 1) {
         System.out.println("Hello not found");
      } else {
         System.out.println("Found Hello at index " + intIndex);
      }
   }
}

Result
The above code sample will produce the following result.

Found Hello at index 0

This example shows how we can search a word within a String object

public class HelloWorld {
   public static void main(String[] args) {
      String text = "The cat is on the table";
      System.out.print(text.contains("the"));
   }
}

Result
The above code sample will produce the following result.

true

How to reverse a String?

Following example shows how to reverse a String after taking it from command line argument .The program buffers the input String using StringBuffer(String string) method, reverse the buffer and then converts the buffer into a String with the help of toString() method.

public class StringReverseExample{
   public static void main(String[] args) {
      String string = "abcdef";
      String reverse = new StringBuffer(string).reverse().toString();
      System.out.println("\nString before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
   }
}

Result
The above code sample will produce the following result.

String before reverse:abcdef
String after reverse:fedcba

Example

Following another example shows how to reverse a String after taking it from command line argument

import java.io.*;
import java.util.*;

public class HelloWorld {
   public static void main(String[] args) {
      String input = "tutorialspoint";
      char[] try1 = input.toCharArray();
      for (int i = try1.length-1;i>=0;i--) System.out.print(try1[i]);
   }
}

The above code sample will produce the following result.

tniopslairotut 

How to replace a substring inside a string by another one ?

This example describes how replace method of java String class can be used to replace character or substring by new one.

public class StringReplaceEmp{
   public static void main(String args[]){
      String str = "Hello World";
      System.out.println( str.replace( 'H','W' ) );
      System.out.println( str.replaceFirst("He", "Wa") );
      System.out.println( str.replaceAll("He", "Ha") );
   }
}


Result
The above code sample will produce the following result.

Wello World
Wallo World
Hallo World

How to remove a particular character from a string ?

Following example shows hoe to remove a character from a particular position from a string with the help of removeCharAt(string,position) method.

public class Main {
   public static void main(String args[]) {
      String str = "this is Java";
      System.out.println(removeCharAt(str, 3));
   }
   public static String removeCharAt(String s, int pos) {
      return s.substring(0, pos) + s.substring(pos + 1);
   }
}


Result
The above code sample will produce the following result.

this is Java

How to search the last position of a substring ?

This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.

public class SearchlastString {
   public static void main(String[] args) {
      String strOrig = "Hello world ,Hello Reader";
      int lastIndex = strOrig.lastIndexOf("Hello");
     
      if(lastIndex == - 1){
         System.out.println("Hello not found");
      } else {
         System.out.println("Last occurrence of Hello is at index "+ lastIndex);
      }
   }
}

Result

The above code sample will produce the following result.

Last occurrence of Hello is at index 13

Example

This another example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.

public class HelloWorld{
   public static void main(String []args) {
      String t1 = "Tutorialspoint";
      int index = t1.lastIndexOf("p");
      System.out.println(index);
   }
}

The above code sample will produce the following result.

9

How to compare two strings in Java ?

Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.

public class StringCompareEmp{
   public static void main(String args[]){
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;

      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );
      System.out.println( str.compareTo(objStr.toString()));
   }
}

Result
The above code sample will produce the following result.

-32
0
0

String compare by equals()

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

public class StringCompareequl{
   public static void main(String []args){
      String s1 = "tutorialspoint";
      String s2 = "tutorialspoint";
      String s3 = new String ("Tutorials Point");
      System.out.println(s1.equals(s2));
      System.out.println(s2.equals(s3));
   }
}

The above code sample will produce the following result.

true 

false 

String compare by == operator
public class StringCompareequl{
   public static void main(String []args){
      String s1 = "tutorialspoint";
      String s2 = "tutorialspoint";
      String s3 = new String ("Tutorials Point");
      System.out.println(s1 == s2);
      System.out.println(s2 == s3);
   }
}

The above code sample will produce the following result.

true

false