Tag Archives: computer software

Can some one give real life example to understand access specifiers in JAVA?

Hello Guys,

Today morning I came across very good information about Object Oriented Programming (OOP) on Quora. I am very excited to share this with you all. As this is not my article, I would like to many thanks to author Pritam Kumar. To read the original post, Can someone give real life example to understand access specifiers in JAVA?

When you update your Facebook Status, it gives you 4 options

1. If you make this status visible for Public, anyone can see this status on Internet (Anyone On or Off Facebook). [Public Access Specifiers]

2. If you make this status visible “only for me”. No one can see this status except you. [Private Access Specifiers]

3. If you make this status visible for Friends or Friends of friends, then your status will be only visible for your friends and your friends friends. Not everyone presents on Facebook or Internet. [Protected Access Specifiers]

4. If you make this status visible for “Friends”, then your status will be only visible for your Friends. Not your friends of friends or everyone presents on Facebook. [Default  Access Specifiers]

In Programming Terms:

There are four Access Specifiers in Java.

1. Public
2. Private
3. Default.
4. Protected

Hope this little information will help you. Feel free to add your valuable comments.
Happy Coding! Stay tuned…

Swapnil Sonar

Android: Some Cool and Useful Intents For Your App

Important intents and their usage..

mcochin

I was just playing around with some INTENTS and wanted to share some useful things they can do.

There are a lot more Intents to be discovered, but maybe this will give you insight on what you can do with your app.

View original post

Android ViewPager and Performance Improvements

Hello Everyone,
Hope you all are doing well. After seeing a threading concept in last post, Thread and Thread interruption in Java Today I would like to share some tricks while using the ViewPageIndicator in android application. So lets start it with following question,

My Activity has swipe-able tabs and each tab calls a different fragment. But when i swipe between tabs the transition seems a quite slow because of the destruction of the fragment view, that is rebuilded during the swipe operation.

The ViewPager doesn’t create all its pages at once. When using a lot of pages this would be horribly slow and even unnecessary if the user would never swipe through all these pages. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page.

offscreen_pages

So what is solution of this problem, Is their any?

Hussshhh.. yeah there is,

 mViewPager.setOffscreenPageLimit(int limit);

If you only use a small amount of pages you may get a better performance by creating them all at once. You can use setOffscreenPageLimit(int limit) to set the number of pages that will be created and retained. With above setting to the pager loads a maximum of 3 pages (fragments) at the time: the one displaying, previous and next so if you have 5 fragments this will happen while you move from first to last: (where X is a loaded fragment)

XX000 -> XXX00 -> 0XXX0 -> 00XXX -> 000XX

If you have more than three tabs/fragments then try to using

 myPager.setOffscreenPageLimit(ITEMS_COUNT-1);

This will tell the pager to keep all of them in memory and not destroy/create with every swipe. But you need to KEEP A CLOSE LOOK ON THE MEMORY MANAGEMENT. For example, there are many many items with image and animations. If you cache all of them, this probably will result with an OutOfMemoryError. To prevent, you can define page limit to be cached.


MINIMUM THRESHOLD :
ViewPager require a minimum of 1 offscreen pages

If you set value to 0 then you should be getting a warning about this in LogCat, something like: Requested offscreen page limit0 too small; defaulting to1
Its because ViewPager is based on concept of keep ready next page to be loaded. ViewPager can not the page which not exist still.

For more close and precise look on this method,
public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. This setting defaults to
Parameters : limit – How many pages will be kept offscreen in an idle state.

Conclusion
So while using ViewPager in Android, if you have a small amount of pages you may get a better performance by creating them all at once and you can also use setOffscreenPageLimit(int limit) to set the number of pages that will be created and retained. But you also need to take care of Memory need if there are more graphics uses.

Hope this is helpful to you! Happy coding..
Stay tuned.

Swapnil Sonar

Interview Questions for IT Fresher| Tech Code Geek

Hello Guys,

Most of the times freshers are confusing while interview, they actually not aware of what type of questions will asked in interview.
I like to share some of Interview Questions which were asked to me during my interview.
Hope it will helpful to you…

What is a CAPTCHA and when do you use it?
CAPTCHA is the short form of Completely Automated Public Turing Test. It’s a step taken to ensure that a feature is not abused in a way it’s not meant to be used. We use CAPTCHAS for general form submissions, registrations, user generated content and so on to prevent spam bots from clogging up the system or spamming.

What is the current version of PHP and MYSQL?
Their current versions are
PHP is 5.4.1 (As of this writing. Make sure you know the current version when you go in for your interview)
MySQL- 5.5.23

Can you tell me the difference between include and require functions?
Both these functions differ in how they handle failures that occur.
Suppose a file is not found by the include() function, then it will simply give a warning and script execution will not be interrupted.
However, if a file is not found using a require() function, it will throw up a fatal warning and stop the script execution.

What the difference is between get and post method in HTTP?
GET Method:
•    All the name value pairs are submitted as a query string in URL. It’s not secured as it is visible in plain text format in the Location bar of the web browser.
•    As the data transfers through address bar (URL) there are some restrictions in using space, some characters like ampersand (&) etc. in the GET method of posting data. We have to take special care for encoding data if such special characters are present.
•    Length of the string is restricted.
•    If method is not mentioned in the Form tag, this is the default method used.
•    If get method is used and if the page is refreshed it would not prompt before the request is submitted again.
•    One can store the name value pairs as bookmark and directly be used while sharing with others – example search results.
•    Data is always submitted in the form of text
•    If the response of the page is always same for the posted query then use GET example database searches

POST Method:
•    All the name value pairs are submitted in the Message Body of the request.
•    Length of the string (amount of data submitted) is not restricted.
•    Post Method is secured because Name-Value pairs cannot be seen in location bar of the web browser.
•    If post method is used and if the page is refreshed it would prompt before the request is resubmitted.
•    If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.
•    Data is submitted in the form as specified in enctype attribute of form tag and thus files can be used in FileUpload input box.

Is it possible to create a database using PHP and MySQL?
Yes. We can create a database using the function mysql_create_db(??).

Is it possible to know details about the browser being used to view a PHP website?
Yes. We can use the get_browser() function to get the details of the browser a user is using. This information is usually found inside the browscap.ini file.

String Concatenation –
There are two different ways to combine variables in PHP. Let’s take an example.
$var1 = “Hello”;
$var2 = “World”;
We can combine it using the two codes below
$var3 = $var1.$var2; and
$var3 = “$var1$var2″;

Method Definitions:-
The set of common methods for HTTP/1.1 is defined below. Although this set can be expanded, additional methods cannot be assumed to share the same semantics for separately extended clients and servers.
Safe and Idempotent Methods
•        OPTIONS
•        GET
•        HEAD
•        POST
•        PUT
•        DELETE
•        TRACE
•        CONNECT

What does setAutoCommit(false) do?
A JDBC connection is created in auto-commit mode by default. This means that each individual SQL statement is treated as a transaction and will be automatically committed as soon as it is executed. If you require two or more statements to be grouped into a transaction then you need to disable auto-commit mode using below command
con.setAutoCommit(false);

Once auto-commit mode is disabled, no SQL statements will be committed until you explicitly call the commit method. A Simple transaction with use of autocommit flag is demonstrated below.

con.setAutoCommit(false);
PreparedStatement updateStmt =
con.prepareStatement( "UPDATE EMPLOYEE SET SALARY = ? WHERE EMP_NAME LIKE?");

updateStmt.setInt(1, 5000); updateSales.setString(2, "Jack");
updateStmt.executeUpdate();

updateStmt.setInt(1, 6000); updateSales.setString(2, "Tom");
updateStmt.executeUpdate();
con.commit();
con.setAutoCommit(true);

What is Connection pooling? What are the advantages of using a connection pool?
Connection Pooling is a technique used for sharing the server resources among requested clients. It was pioneered by database vendors to allow multiple clients to share a cached set of connection objects that provides access to a database.
Getting connection and disconnecting are costly operation, which affects the application performance, so we should avoid creating multiple connections during multiple database interactions.
A pool contains set of Database connections which are already connected, and any client who wants to use it can take it from pool and when done with using it can be returned back to the pool.
Apart from performance this also saves you resources as there may be limited database connections available for your application.

What is a stored procedure? How to call stored procedure using JDBC API?
Stored procedure is a group of SQL statements that forms a logical unit and performs a particular task.
Stored Procedures are used to encapsulate a set of operations or queries to execute on database. Stored procedures can be compiled and executed with different parameters and results and may have any combination of input/output parameters. Stored procedures can be called using CallableStatement class in JDBC API.
Below code snippet shows how this can be achieved.

CallableStatement cs = con.prepareCall("{call MY_STORED_PROC_NAME}");
ResultSet rs = cs.executeQuery();

What are the types of statements in JDBC?
The JDBC API has 3 Interfaces,
1.    Statement
2.    PreparedStatement,
3.    CallableStatement

The key features of these are as follows:

Statement
This interface is used for executing a static SQL statement and returning the results it produces.
The object of Statement class can be created using Connection.createStatement() method.

PreparedStatement
A SQL statement is pre-compiled and stored in a PreparedStatement object.
This object can then be used to efficiently execute this statement multiple times.
The object of PreparedStatement class can be created using Connection.prepareStatement() method. This extends Statement interface.

CallableStatement
This interface is used to execute SQL stored procedures.
This extends PreparedStatement interface.
The object of CallableStatement class can be created using Connection.prepareCall() method.

What does Class.forName() method do?
Method forName() is a static method of java.lang.Class. This can be used to dynamically load a class at run-time. Class.forName() loads the class if it’s not already loaded. It also executes the static block of loaded class. Then this method returns an instance of the loaded class. So a call to Class.forName(‘MyClass’) is going to do following

– Load the class MyClass.
– Execute any static block code of MyClass.
– Return an instance of MyClass.

JDBC Driver loading using Class.forName is a good example of best use of this method. The driver loading is done like this

Class.forName("org.mysql.Driver");

All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static initializer method registerDriver() which can be called in a static blocks of Driver class. A MySQL JDBC Driver has a static initializer which looks like this:

static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}

Class.forName() loads driver class and executes the static block and the Driver registers itself with the DriverManager.

Good Luck.. 🙂
Swapnil Sonar