Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, October 2, 2011

Free SMS India - Android and J2ME Apps - For Way2SMS

Want to send free sms in india using way2sms now do not need to use your desktop or laptop. You can download the mobile applications to send the sms free in india naywhere. If you have android operating system mobile or java enabled mobile.

Android OS mobile
You need to download the .apk file from http://way2smsapps.codeplex.com/releases/62599/download/217544
and install it on your mobile. Create account with way2sms and use those credentials for sending free sms anywhere in india.

Java Enabled mobile
You need to download the .jar file from http://way2smsapps.codeplex.com/releases/62563/download/217412
and install it on your mobile. Create account with way2sms and use those credentials for sending free sms anywhere in india.

Thursday, May 12, 2011

Constructor Chaining

We know that constructors are invoked at runtime when you say new on some class type as follows:
Sparrow sparrow = new Sparrow();

Now we will see what really happens when we say new Sparrow()?
(Assume Sparrow extends Bird and Bird extends Object.)
  1. Sparrow constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(), unless the constructor invokes an overloaded constructor of the same class (more on that in a minute). 
  2. Bird constructor is invoked (Bird is the superclass of Sparrow).
  3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Bird extends Object even though you don't actually type "extends Object" into the Bird class declaration. It's implicit.) At this point we're on the top of the stack.
  4. Object instance variables are given their explicit values. By explicit values,we mean values that are assigned at the time the variables are declared,like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable.
  5. Object constructor completes.
  6. Bird instance variables are given their explicit values (if any).
  7. Bird constructor completes
  8. Sparrow instance variables are given their explicit values (if any). 
  9. Sparrow constructor completes.
You can find below,  how constructors are working in call stack

  1. Object()
  2.  Bird() calls super()
  3.  Sparrow() calls super()
  4. main() calls  new Sparrow()


Tuesday, May 3, 2011

SAR (Service Archive)

SAR file is created with the .sar extenstion same like how other archive files (JAR, WAR, EAR) created. But the purpose of the SAR file is differenet from the other archive files. It is used for deploying a service component in the application server without dependent on other componenets. You can create a seperate componenet as a SAR file and deploy it in the server. When application server starts, the component will be deployed and started running independently. Most of the times these SAR files are used for writing thread related components which has to be running independently.

JBoss's service archive architecture is based on the Java Extension Management (JMX). During the server bootup process, the service archive deployer (SARDeployer) instantiates the JBoss service classes and exposes them as manageable beans through JMX. You can view all of registered JBoss service components in the JBoss JMX Console web application.

Friday, April 29, 2011

HTTP methods and the corresponding servlet methods

HTTP method       HttpServlet method

GET                        doGet()
HEAD                    doHead()
POST                     doPost()
PUT                       doPut()
DELETE                doDelete()
OPTIONS             doOptions()
TRACE                  doTrace()

Monday, April 18, 2011

Nested Classes

A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class's members even if they are declared private.
The following table shows the types of nested classes: 

Types of Nested Classes
Type Scope Inner
static nested class member no
inner [non-static] class member yes
local class local yes
anonymous class only the point where it is defined yes

Wednesday, April 6, 2011

Use of persistence.xml file

The EntityManager API is great, but how does the server/ application know which database it is supposed to save / update / query the entity objects?
How do we configure the underlying object-relational-mapping engine and cache for better performance and trouble shooting? The persistence.xml file gives you complete flexibility to configure the EntityManager.
  • The persistence.xml file is a standard configuration file in JPA.
  • It has to be included in the META-INF directory inside the JAR file that contains the entity beans.
  • The persistence.xml file must define a persistence-unit with a unique name in the current scoped classloader.
  • The provider attribute specifies the underlying implementation of the JPA EntityManager. 
  • In JBoss AS, the default and only supported / recommended JPA provider is Hibernate.
  • The jta-data-source points to the JNDI name of the database this persistence unit maps to e.g. java:/DefaultDS.

Java Display Image Gray

JButton  photoBtn = new JButton();
File file = new File("C:/photos/myImage.jpg");
BufferedImage myImage = ImageIO.read(file);
BufferedImage imageGray = new BufferedImage(myImage.getWidth(), myImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = imageGray.getGraphics();
g.drawImage(myImage, 0, 0, 200, 200, null);
g.dispose();
photoBtn.setIcon(new ImageIcon(imageGray));
photoBtn.revalidate();

The above code display colored image as gray in button as icon.

Tuesday, April 5, 2011

Java 5 Feature : Formatted Output: printf

Takes a variable number of arguments
– System.out.printf("Formatting String", arg1, arg2, …);
• Advantages
– Lets you insert values into output without much clumsier String concatenation.
– Lets you control the width of results so things line up
– Lets you control the number of digits after the decimal point in numbers, for consistent-looking output
• Very similar to C/C++ printf function
– If you know printf in C/C++, you can probably use Java's printf immediately without reading any documentation
        • Although some additions in time formatting and locales
– Use String.format to get the equivalent of C's sprintf

Monday, April 4, 2011

Java reserved words for literal values


false
A boolean literal value.
null
A reference literal value.
true
A boolean literal value.

Saturday, April 2, 2011

Java Basics


A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types.


  • Class A template that describes the kinds of state and behavior that objects of its type support. 

  • Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.

  • State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.



  • Behavior (methods) When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Sunday, March 13, 2011

JAVA 7 Features


Virtul Machine

JSR 292: Support for dynamically-typed languages (InvokeDynamic)

VM and language extensions to support the implementation of dynamically-typed languages at performance levels near to that of the Java language itself

Strict class-file checking [NEW]
Per the Java SE 6 specification, class files of version 51 (SE 7) or later must be verified with the typechecking verifier introduced by JSR 202 in Java SE 6; the VM must not fail over to the old inferencing verifier

Language

JSR 334: Small language enhancements (Project Coin)

A set of small language changes intended to simplify common, day-to-day programming tasks:

1.      Strings in switch statements
2.      Automatic resource management
3.      Improved type inference for generic instance creation ("diamond")
4.      Simplified varargs method invocation,
5.      Better integral literals,
6.       Improved exception handling (multi-catch)

Core

Upgrade class-loader architecture

Modifications to the ClassLoader API and implementation to avoid deadlocks in non-hierarchical class-loader topologies

Method to close a URLClassLoader

A method that frees the underlying resources, such as open files, held by a URLClassLoader

Concurrency and collections updates (jsr166y)

A lightweight fork/join framework, flexible and reusable synchronization barriers, transfer queues, a concurrent-reference HashMap, and thread-local pseudo-random number generators

Internationalization

Unicode 6.0

Upgrade the supported version of Unicode to 6.0

Locale enhancement

Upgrade the java.util.Locale class to support IETF BCP 47 and UTR 35 (CLDR/LDML)

Separate user locale and user-interface locale

Upgrade the handling of locales to separate formatting locales from user-interface language locales, as is done on Vista and later versions of Windows

I/O and Networking

JSR 203: More new I/O APIs for the Java platform (NIO.2)

New APIs for filesystem access, scalable asynchronous I/O operations, socket-channel binding and configuration, and multicast datagrams

NIO.2 filesystem provider for zip/jar archives

A fully-functional and supported NIO.2 filesystem provider for zip and jar files

SCTP (Stream Control Transmission Protocol)

An implementation-specific API for the Stream Control Transmission Protocol on Solaris

SDP (Sockets Direct Protocol)

Implementation-specific support for reliable, high-performance network streams over Infiniband connections on Solaris and Linux

Use the Windows Vista IPv6 stack

Upgrade the networking code to use the Windows Vista IPv6 stack, when available, in preference to the legacy Windows stack

TLS 1.2

Add support for TLS 1.2, which was standardized in 2008 as RFC 5246

Security & Cryptography

Elliptic-curve cryptography (ECC)

A portable implementation of the standard Elliptic Curve Cryptographic (ECC) algorithms, so that all Java applications can use ECC out-of-the-box

JDBC

JDBC 4.1

Upgrade to JDBC 4.1 and Rowset 1.1

Client

XRender pipeline for Java 2D

A new Java2D graphics pipeline based upon the X11 XRender extension, which provides access to much of the functionality of modern GPUs

Create new platform APIs for 6u10 graphics features

Create new platform APIs for features originally implemented in the 6u10 release: Translucent and shaped windows, heavyweight/lightweight mixing, and the improved AWT security warning

Nimbus look-and-feel for Swing

A next-generation cross-platform look-and-feel for Swing

Swing JLayer component

Add the SwingLabs JXLayer component decorator to the platform

Web

Update the XML stack

Upgrade the JAXP, JAXB, and JAX-WS APIs to the most recent stable versions

Management

Enhanced JMX Agent and MBeans [NEW]

An implementation-specific enhanced JMX management agent, ported from JRockit, which makes it easier to connect to the platform MBean server through firewalls, together with a richer set of MBeans which expose additional information about the internal operation of the VM

Deferred to JDK 8 or later

JSR 294: Language and VM support for modular programming

Enhancements to the Java language and virtual-machine specifications to support modular programming, at both compile time and run time

JSR 308: Annotations on Java types

An extension to the Java annotation syntax to permit annotations on any occurrence of a type

JSR TBD: Language support for collections

Literal expressions for immutable lists, sets, and maps, and indexing-access syntax for lists and maps

JSR TBD: Project Lambda

Lambda expressions (informally, "closures") and defender methods for the Java programming language

Modularization (Project Jigsaw)

A simple, low-level module system focused upon the goal of modularizing the JDK, and the application of that system to the JDK itself

JSR 296: Swing application framework

An API to define the basic structure of a typical Swing application, thereby eliminating lots of boilerplate code and providing a much-improved initial developer experience

Swing JDatePicker component

Add the SwingLabs JXDatePicker component to the platform



Monday, May 10, 2010

Volatile Modifiers in Java

Volatile Modifier

       The volatile modifier requests the Java Virtual Machine to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, and one or more threads might update that variable’s value, and all of the threads do not use synchronization to read and/or write the variable value, then that member variable must be declared volatile to ensure all threads should get the updated value.
       We will discuss the java volatile modifier using following example.
       In our example, we take two threads Thread T1 and Thread T2 accessing member variable x of class C and here Thread T1 is not using synchronization and Thread T2 uses synchronization. If Thread T2 updates value of variable x from 0 to 1. But if the variable x is not declared as volatile then meanwhile Thread T1 tried to access variable then it will get value of variable x as 0. But as variable x is updated to 1 by Thread T2, To avoid this mess the variable x should be declared as volatile so Thread T1 should get updated value 1.

Friday, April 30, 2010

Java pass by value or pass by reference

  • Java manipulates objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
  • Java is strictly pass-by-value, exactly as in C.
  • Java has pointers and is strictly pass-by-value.


Friday, February 12, 2010

Servlet Life Cycle

Servlet Life Cycle Methods

The following are the life cycle methods of a servlet instance:
  • init()
  • service()
  • destroy()
We will look into the each method in detail.

init()

This method is called once for a servlet instance. When first time servlet is called, servlet container creates instance of that servlet and loaded into the memory. Future requests will be served by the same instance without creating the new instance. Servlet by default multithreaded application.init() method is used for inilializing servlet variables which are required to be passed from the deployment descriptor web.xml. ServletConfig is passed as the parameter to init() method which stores all the values configured in the web.xml. It is more convenient way to initialize the servlet.

service()

This method is called for the each request. This is the entry point for the every servlet request and here we have to write our businesslogic or any other processes. This method takes HttpServletRequest and HttpServletresponse as the parameters. It is not mandatory to write this method, normally developers are interested in writing doGet() or doPost() methods which is by default called from the service() method. If you override service(), it is your reponsibility to call the appropriate methods. If you are not overridden the service() method, based on the types of the request the methods will be called.

destroy()

This method will be called once for a instance. It is used for releasing any resources used by the servlet instance. Most of the times it could be database connections, Fill IO operations, etc. destroy() is called by the container when it is removing the instance from the servlet container. Servlet instance is deleted or garbage collected by the container only when the web server issues shut down or the instance is not used for a long time.