<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Java Interview Questions</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-interview-questions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 30 Jul 2010 06:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Exception Handling and their uses</title>
		<link>http://www.bestjavainterviewquestions.com/exception-handling-and-their-uses/</link>
		<comments>http://www.bestjavainterviewquestions.com/exception-handling-and-their-uses/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 16:31:51 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Java Interview Questions]]></category>
		<category><![CDATA[exception handling]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=587</guid>
		<description><![CDATA[Q)  What is the difference between ‘throw’ and ‘throws’ ?And it’s application? Rep) Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an exception that it does not [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q)  What is the difference between ‘throw’ and ‘throws’ ?And it’s application?<br />
</strong></p>
<p><strong>Rep) </strong> Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an exception that it does not<br />
handle, it must specify this behavior so the callers of the method can guard<br />
against that exception.</p>
<p><strong>Q)   What is the difference between ‘Exception’ and ‘error’ in java?</strong></p>
<p><strong>Rep) </strong> Exception and Error are the subclasses of the Throwable class. Exception class is used for exceptional conditions that user program should catch. With exception class we can subclass to create our own custom exception.<br />
Error defines exceptions that are not excepted to be caught by you program. Example is Stack Overflow.</p>
<p><strong>Q)  What is ‘Resource leak’?<br />
</strong></p>
<p><strong>Rep) </strong> Freeing up other resources that might have been allocated at the beginning of a method.</p>
<p><strong>Q)  What is the ‘finally’ block?</strong></p>
<p><strong>Rep) </strong> Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.</p>
<p><strong>Q)  Can we have catch block with out try block? If so when?<br />
</strong></p>
<p><strong>Rep) </strong> No. Try/Catch or Try/finally form a unit.</p>
<p><strong>Q)  What will happen to the Exception object after exception handling?<br />
</strong></p>
<p><strong>Rep) </strong> It will go for Garbage Collector. And frees the memory.</p>
<p><strong>Q)  How many Exceptions we can define in ‘throws’ clause?<br />
</strong></p>
<p><strong>Rep) </strong> We can define multiple exceptions in throws clause.<br />
Signature is.. : type method-name (parameter-list) throws exception-list</p>
<p><strong>Q)  The finally block is executed when an exception is thrown, even if no catch matches it.<br />
True/False<br />
</strong></p>
<p><strong>Rep) </strong> True</p>
<p><strong>Q)   The subclass exception should precede the base class exception when used within the catch clause.<br />
True/False<br />
</strong></p>
<p><strong>Rep) </strong>True</p>
<p><strong>Q)   Exceptions can be caught or rethrown to a calling method.<br />
True/False</strong></p>
<p><strong>Rep) </strong>True</p>
<p><strong>Q)  The statements following the throw keyword in a program are not executed.<br />
True/False</strong></p>
<p><strong>Rep) </strong> True</p>
<p><strong>Q)  The toString ( ) method in the user-defined exception class is overridden.<br />
True/False</strong></p>
<p><strong>Rep) </strong>True</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/define-jsp/" title="Define JSP"><img src="Array" alt="Define JSP" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/ejb-interview-questions-2/" title="EJB Interview questions"><img src="Array" alt="EJB Interview questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/difference-between-include-and-import/" title="Difference between #include and Import"><img src="Array" alt="Difference between #include and Import" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/different-types-of-session-tracking/" title="Different types of Session Tracking"><img src="Array" alt="Different types of Session Tracking" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/how-to-get-a-leap-year/" title="How to get a Leap Year"><img src="Array" alt="How to get a Leap Year" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/exception-handling-and-their-uses/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to get a Leap Year</title>
		<link>http://www.bestjavainterviewquestions.com/how-to-get-a-leap-year/</link>
		<comments>http://www.bestjavainterviewquestions.com/how-to-get-a-leap-year/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 14:31:38 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Java Interview Questions]]></category>
		<category><![CDATA[java leapyear code]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=544</guid>
		<description><![CDATA[import java.util.Calendar.*; import java.lang.*;  //its&#8217; a default package which will import class LeapYear { public static void main(String args[])throws Exception { Calendar cal=Calendar.getInstance(); int year=cal.get(Calendar.YEAR); if(year % 4 == 0 &#38;&#38; (year % 100 != 0 &#124;&#124; year % 400 == 0)) {   System.out.println(&#8220;Leap Year&#8221;);          }else  { System.out.println(&#8220;Not a Leap Year);      } } }  // [...]]]></description>
			<content:encoded><![CDATA[<p>import java.util.Calendar.*;</p>
<p>import java.lang.*;  //its&#8217; a default package which will import</p>
<p>class <strong>LeapYear </strong>{</p>
<p>public static void main(String args[])throws Exception {</p>
<p>Calendar cal=Calendar.getInstance();</p>
<p>int year=cal.get(Calendar.YEAR);</p>
<p>if(year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0))</p>
<p>{   System.out.println(&#8220;Leap Year&#8221;);          }else  {</p>
<p>System.out.println(&#8220;Not a Leap Year);      }</p>
<p>}</p>
<p>}  // The Meaning of above is every 4 year&#8217;s the LeapYear comes and when The Centuries comes into pictures it will divided by the 400 so that the remainder should be zera is the Leap year</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/creating-a-stateful-session-bean/" title="Creating a Stateful session Bean"><img src="Array" alt="Creating a Stateful session Bean" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/java-basics-interview-questions/" title="Java Basics Interview Questions"><img src="Array" alt="Java Basics Interview Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/collection-framework/" title="Collection FrameWork"><img src="Array" alt="Collection FrameWork" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions-2/" title="J2EE Interview Questions"><img src="Array" alt="J2EE Interview Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/using-serialization/" title="Using Serialization "><img src="Array" alt="Using Serialization " /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/how-to-get-a-leap-year/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Define Jar,War,Ear file</title>
		<link>http://www.bestjavainterviewquestions.com/define-jarwarear-file/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-jarwarear-file/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 19:04:02 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Define creating and extracting of jar]]></category>
		<category><![CDATA[ear]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[war]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=578</guid>
		<description><![CDATA[Q) What is EAR file Rep) Enterprise Archive file. A JAR archive that contains a J2EE application. Q) What is JAR file Rep) Java Archive file it contains a J2EE application. You can create a JAR file using the command jar cf XYZ.jar *.class *.gif if will compress all the .class files and the related [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q) What is EAR file</strong></p>
<p><strong>Rep) </strong>Enterprise Archive file. A JAR archive that contains a<br />
J2EE application.</p>
<p><strong>Q) What is JAR file</strong></p>
<p><strong>Rep)</strong> Java Archive file it contains a J2EE application.</p>
<p>You can create a JAR file using the command</p>
<p><em>jar cf XYZ.jar *.class *.gif</em></p>
<p>if will compress all the .class files and the related gif files to the one file called jar</p>
<p>You can extract the JAR file using the command</p>
<p><em>jar xf XYZ.jar</em></p>
<p>Updating the existing JAR file</p>
<p><em>jar -uf XYZ.jar file1.class</em></p>
<p><strong>Q) What is WAR file</strong></p>
<p><strong>Rep)</strong> Enterprise Archive file. A JAR archive that contains a<br />
J2EE application.</p>
<p>You can create a <strong>WAR </strong>file using the command</p>
<p><em>jar -cfv XYZ.war</em></p>
<p>You can extract a <strong>WAR </strong>file using the command</p>
<p><em>jar -xfv XYZ.war</em></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/define-earjar-and-war/" title="Define Ear,jar and war"><img src="Array" alt="Define Ear,jar and war" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-jarwarear-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploading Excel File  Using Jsp</title>
		<link>http://www.bestjavainterviewquestions.com/uploading-excel-file-using-jsp/</link>
		<comments>http://www.bestjavainterviewquestions.com/uploading-excel-file-using-jsp/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 16:27:39 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Upload Excel sheet using jsp/java]]></category>
		<category><![CDATA[uploading the excelfile]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=541</guid>
		<description><![CDATA[There are two choice for reading and writing the Excel file in your application. 1.By Using the JExcelapi 2.By Jakarta&#8217;s Poi.jar which uses the HSSFSheet read and write. &#8211;&#62; Using the JExcelapi which  is  not suitable for the important data. It fails to read several files and  in most occasions it also fails to create [...]]]></description>
			<content:encoded><![CDATA[<p>There are two choice for reading and writing the Excel file in your application.</p>
<p>1.By Using the JExcelapi</p>
<p>2.By Jakarta&#8217;s Poi.jar which uses the HSSFSheet read and write.</p>
<p>&#8211;&gt; Using the JExcelapi which  is  not suitable for the important data. It fails to read several files and  in most occasions it also fails to create cells. In short JExcelAPI isn&#8217;t suitable for enterprise use.</p>
<p>&#8211;&gt;Where as the POI.jar is a Jakarta  Project&#8217;s which was developed  by pure Java implementation of the Excel file format. It is a mature product and was able to correctly and effortlessly read excel data generated from various sources, including non-MS Excel products like Open Office, and for various versions of Excel. so for this reasons it is Highly recommended.</p>
<p>Before Using this Code you have to download the <strong><em>poi.jar</em></strong> file  and place it in the <em>lib</em> folder of your application (or) place in the jar folder and give the path in the <em>environment variables in the My Computers</em>, and then import the <em>HSSF</em> files and then follow the code</p>
<p><em>&lt;%@ page import = &#8220;java.io.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;java.sql.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;javax.servlet.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;javax.servlet.http.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;java.util.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;java.awt.*&#8221; %&gt;<br />
&lt;%@ page import = &#8220;java.io.File&#8221; %&gt;<br />
&lt;%@ page import = &#8220;java.io.IOException&#8221; %&gt;<br />
&lt;%@ page import = &#8220;javax.swing.filechooser.FileSystemView&#8221; %&gt;</p>
<p>&lt;%@ page import=&#8221;org.apache.poi.hssf.usermodel.HSSFSheet&#8221;%&gt;<br />
&lt;%@ page import=&#8221;org.apache.poi.hssf.usermodel.HSSFWorkbook&#8221;%&gt;<br />
&lt;%@ page import=&#8221;org.apache.poi.hssf.usermodel.HSSFRow&#8221;%&gt;<br />
&lt;%@ page import=&#8221;org.apache.poi.hssf.usermodel.HSSFCell&#8221;%&gt;</p>
<p>&lt;%<br />
//Here Datedas is the value of the length which is retrived from the database in my application</em></p>
<p><em><br />
String fname1=&#8221;File&#8221;-&#8221;+Datedas+&#8221;.xls&#8221;; </em></p>
<p><em>//Giving name to the file<br />
</em></p>
<p><em> //creating a file object</em></p>
<p><em> File fname=new File(fname1);</em></p>
<p><em>//Creating an object to the HSSF Class<br />
</em></p>
<p><em> HSSFWorkbook hwb = new HSSFWorkbook();<br />
</em></p>
<p>//Giving the name to the Excel file which will be created by this HSSFsheet</p>
<p><em>HSSFSheet sheet = hwb.createSheet(&#8220;stock sheet&#8221;);<br />
</em></p>
<p><em>//Creating the row for your data</em></p>
<p><em>HSSFRow pw = sheet.createRow((short)2);<br />
</em></p>
<p><em>//Mention the Heading of your Excel sheet<br />
</em></p>
<p><em> pw.createCell((short) 0).setCellValue(&#8220;S.No&#8221;);<br />
pw.createCell((short) 1).setCellValue(&#8220;Heading1&#8243;);<br />
pw.createCell((short) 2).setCellValue(&#8220;</em><em>Heading2</em><em>&#8220;);<br />
pw.createCell((short) 3).setCellValue(&#8220;</em><em>Heading3</em><em>&#8220;);<br />
pw.createCell((short) 4).setCellValue(&#8220;</em><em>Heading4</em><em>&#8220;);<br />
pw.createCell((short) 5).setCellValue(&#8220;</em><em>Heading5</em><em>&#8220;);<br />
pw.createCell((short) 6).setCellValue(&#8220;</em><em>Heading6</em><em>&#8220;);<br />
pw.createCell((short) 7).setCellValue(&#8220;</em><em>Heading7</em><em>&#8220;);<br />
pw.createCell((short) 8).setCellValue(&#8220;</em><em>Heading8</em><em>&#8220;);<br />
pw.createCell((short) 9).setCellValue(&#8220;</em><em>Heading9</em><em>&#8220;);<br />
pw.createCell((short) 10).setCellValue(&#8220;</em><em>Heading10</em><em>&#8220;);<br />
pw.createCell((short) 11).setCellValue(&#8220;</em><em>Heading1</em><em>1&#8243;);</p>
<p>//String stored1=&#8221;",stored2=&#8221;",stored3=&#8221;",stored4=&#8221;",stored5=&#8221;",stored6=&#8221;",stored7=&#8221;",stored9=&#8221;";<br />
//float stored8=0.0f,stored10=0.0f,stored12=0.0f;<br />
int comprk=1;</p>
<p>int index=3;<br />
//int compno=0;<br />
int sno=0;<br />
while(rs.next())<br />
{</p>
<p>compno=1;<br />
sno++;</p>
<p>System.out.println(&#8220;Testing in the loop  &#8220;+compno);<br />
</em></p>
<p><em>//Creating the Excel Sheet Rows </em></p>
<p><em>HSSFRow row = sheet.createRow((short)index);</em></p>
<p><em> row.createCell((short) 0).setCellValue(sno);</em></p>
<p><em>row.createCell((short)                                                    .setCellValue(Integer.parseInt(rs.getString(1)));<br />
row.createCell((short) 2).setCellValue(rs.getString(2));<br />
row.createCell((short) 3).setCellValue(rs.getString(3));<br />
row.createCell((short) 4).setCellValue(rs.getString(4));<br />
row.createCell((short) 5).setCellValue(rs.getString(5));<br />
row.createCell((short) 6).setCellValue(rs.getString(6));<br />
row.createCell((short) 7).setCellValue(rs.getString(7));<br />
row.createCell((short) 8).setCellValue(rs.getFloat(8));<br />
row.createCell((short) 9).setCellValue(rs.getString(9));<br />
row.createCell((short) 10).setCellValue(rs.getFloat(10));<br />
row.createCell((short) 11).setCellValue(rs.getFloat(13));<br />
index++;<br />
}</p>
<p>FileOutputStream fileOut = new FileOutputStream(fname);</p>
<p>hwb.write(fileOut);<br />
fileOut.flush();<br />
fileOut.close();<br />
out.println(&#8220;&lt;b&gt;Your excel file has been Successfully generated&lt;/b&gt;&#8221;);</p>
<p>%&gt;</em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/difference-between-include-and-import/" title="Difference between #include and Import"><img src="Array" alt="Difference between #include and Import" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/jsp-scriplets-and-templets/" title="Jsp Scriplets and Templets"><img src="Array" alt="Jsp Scriplets and Templets" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/java-garbage-collections/" title="Java Garbage Collections"><img src="Array" alt="Java Garbage Collections" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/what-are-the-elements-of-jsp/" title="What are the elements of JSP"><img src="Array" alt="What are the elements of JSP" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/jdbc-interview-questions/" title="Jdbc Interview Questions"><img src="Array" alt="Jdbc Interview Questions" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/uploading-excel-file-using-jsp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploading file name or image to DataBase</title>
		<link>http://www.bestjavainterviewquestions.com/uploading-file-name-of-image-to-database/</link>
		<comments>http://www.bestjavainterviewquestions.com/uploading-file-name-of-image-to-database/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 17:05:53 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[FileUpload to DataBase]]></category>
		<category><![CDATA[FileUpload]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=535</guid>
		<description><![CDATA[This is an example of the file Uploading in Java using Servlets . This file uploading concept Works with the Java as follows Here is the Code of the file Uploading using Servlets in JAVA and saving the image and filename of image in the database Name the HTML page as file.html &#60;!DOCTYPE HTML PUBLIC [...]]]></description>
			<content:encoded><![CDATA[<p>This is an example of the file Uploading in Java using Servlets . This file uploading concept Works with the Java as follows</p>
<p>Here is the Code of the file Uploading using Servlets in JAVA and saving the image and filename of image in the database</p>
<p><strong>Name the HTML page as file.html</strong></p>
<p><em>&lt;!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”&gt;<br />
&lt;html&gt;<br />
&lt;form method=”post” action=”servlet/UploadFile” enctype=”multipart/form-data”&gt;<br />
Name<br />
&lt;input type=”text” name=”uname”/&gt;<br />
File<br />
&lt;input type=”file” name=”upfile”/&gt;<br />
&lt;input type=”submit”/&gt;<br />
&lt;/form&gt;<br />
&lt;/html&gt;</em></p>
<p>Write the servlet code and specify the name of the servlet as <strong><em>UploadFile.java</em></strong></p>
<p><em>package com.struts;</em></p>
<p><em>import java.io.*;<br />
import java.sql.*;<br />
import javax.servlet.http.HttpServlet;<br />
import javax.servlet.http.HttpServletRequest;<br />
import javax.servlet.http.HttpServletResponse;<br />
import javax.servlet.ServletInputStream.*;<br />
import java.io.PrintWriter;</em></p>
<p><em>public class UploadFile extends HttpServlet {</em></p>
<p><em>public void doPost(HttpServletRequest req,HttpServletResponse res)<br />
{</em></p>
<p><em>try{</em></p>
<p><em>Class.forName(&#8220;com.mysql.jdbc.Driver&#8221;);<br />
Connection conn = DriverManager.getConnection(&#8220;jdbc:mysql://localhost/test?user=root&amp;password=test&#8221;);<br />
PrintWriter out=res.getWriter();</em></p>
<p><em>//out.println(&#8220;&lt;br&gt;Content type is :: &#8221; +contentType);<br />
//to get the content type information from JSP Request Header<br />
String contentType = req.getContentType();<br />
int flag=0;<br />
FileInputStream fis=null;<br />
FileOutputStream fileOut=null;<br />
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0<br />
if ((contentType != null) &amp;&amp; (contentType.indexOf(&#8220;multipart/form-data&#8221;) &gt;= 0))<br />
{<br />
DataInputStream in = new DataInputStream(req.getInputStream());<br />
//we are taking the length of Content type data<br />
int formDataLength = req.getContentLength();<br />
byte dataBytes[] = new byte[formDataLength];<br />
int byteRead = 0;<br />
int totalBytesRead = 0;</em></p>
<p><em>//this loop converting the uploaded file into byte code<br />
while (totalBytesRead &lt; formDataLength) {<br />
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);<br />
totalBytesRead += byteRead;<br />
}</em></p>
<p><em>String file = new String(dataBytes);<br />
//for saving the file name<br />
String saveFile = file.substring(file.indexOf(&#8220;filename=\&#8221;") + 10);<br />
saveFile = saveFile.substring(0, saveFile.indexOf(&#8220;\n&#8221;));<br />
out.println(&#8220;savefiledddd    &#8220;+saveFile);<br />
int extension_save=saveFile.lastIndexOf(&#8220;\&#8221;");<br />
String extension_saveName=saveFile.substring(extension_save);</em></p>
<p><em>//Here we are invoking the absolute path out of the encrypted data</em></p>
<p><em>saveFile = saveFile.substring(saveFile.lastIndexOf(&#8220;\\&#8221;)+ 1,saveFile.indexOf(&#8220;\&#8221;"));<br />
int lastIndex = contentType.lastIndexOf(&#8220;=&#8221;);<br />
String boundary = contentType.substring(lastIndex + 1,contentType.length());<br />
int pos;</em></p>
<p><em>//extracting the index of file<br />
pos = file.indexOf(&#8220;filename=\&#8221;");<br />
pos = file.indexOf(&#8220;\n&#8221;, pos) + 1;<br />
pos = file.indexOf(&#8220;\n&#8221;, pos) + 1;<br />
pos = file.indexOf(&#8220;\n&#8221;, pos) + 1;<br />
int boundaryLocation = file.indexOf(boundary, pos) &#8211; 4;<br />
int startPos = ((file.substring(0, pos)).getBytes()).length;<br />
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;</em></p>
<p><em>out.println(&#8220;savefile&#8221;+saveFile);</em></p>
<p><em>int file_No=22;<br />
String pathname_dir=&#8221;D:\\rk\\&#8221;+saveFile;<br />
File filepath=new File(pathname_dir);<br />
out.println(&#8220;filepath_  &#8220;+filepath);<br />
fileOut = new FileOutputStream(filepath);<br />
fileOut.write(dataBytes, startPos, (endPos &#8211; startPos));<br />
fileOut.flush();<br />
out.println(&#8220;&lt;h1&gt; your files are saved&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&#8221;);<br />
out.close();</em></p>
<p><em>File database_filename=new File(pathname_dir);<br />
fis=new FileInputStream(database_filename);<br />
PreparedStatement ps = conn.prepareStatement(&#8220;insert into test (fname) values (?)&#8221;);<br />
ps.setString(1,database_filename.getName());<br />
ps.executeUpdate();<br />
ps.close();<br />
flag=1;</em></p>
<p><em>}</em></p>
<p><em>if(flag==1)<br />
{<br />
fileOut.close();<br />
fis.close();<br />
}<br />
}catch(Exception e)<br />
{<br />
System.out.println(&#8220;Exception Due to&#8221;+e);<br />
e.printStackTrace();<br />
}<br />
}<br />
}</em></p>
<p>The above code is used to save the path in the DataBase where(in Which directory/folder) the file is saved .</p>
<p>If you want to save the image into data base the just modify the part of the above code like the below specified.</p>
<p>Here note that the image column data type should be take as blob</p>
<p><em> File database_filename=new File(pathname_dir);<br />
fis=new FileInputStream(database_filename);<br />
PreparedStatement ps = conn.prepareStatement(&#8220;insert into test (fname,image) values (?,?)&#8221;);<br />
ps.setString(1,database_filename.getName());<br />
ps.setBinaryStream(2,fis,database_filename.length())</em></p>
<p><em>//ps.setBinaryStream(int parameterIndex,InputStream x,int length)</em></p>
<p><em>to know more about BinaryStream and the methods of sql package <a class="wp-caption" title="methods of sql Interface" href="http://computerpreferedcourses.blogspot.com/2009/01/javasql-interface-preparedstatement.html" target="_blank">clickhere</a><br />
ps.executeUpdate();<br />
ps.close();</em></p>
<p>Here is Deployment discriptor to pass the request from the html to Servlet program. save these with the name <strong>web.xml </strong>in the <strong>WEB-INF</strong> directory</p>
<p><em> &lt;servlet&gt;<br />
&lt;servlet-name&gt;UploadFile&lt;/servlet-name&gt;<br />
&lt;servlet-class&gt;com.myapp.struts.UploadFile&lt;/servlet-class&gt;<br />
&lt;/servlet&gt;</em></p>
<p><em> &lt;servlet-mapping&gt;<br />
&lt;servlet-name&gt;UploadFile&lt;/servlet-name&gt;<br />
&lt;url-pattern&gt;/servlet/UploadFile&lt;/url-pattern&gt;<br />
&lt;/servlet-mapping&gt;</em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/explain-executeupdate-method/" title="Explain executeUpdate Method"><img src="Array" alt="Explain executeUpdate Method" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/adding-a-database-driver-in-hibernate/" title="Adding a DataBase Driver in Hibernate"><img src="Array" alt="Adding a DataBase Driver in Hibernate" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/explain-session-tracking-using-servlet/" title="Explain Session tracking using Servlet"><img src="Array" alt="Explain Session tracking using Servlet" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/difference-between-sessionupdate-and-sessionlock/" title="Difference between session.update() and session.lock()"><img src="Array" alt="Difference between session.update() and session.lock()" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/write-a-note-on-ejb-frame-work/" title="Write a note on EJB frame work"><img src="Array" alt="Write a note on EJB frame work" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/uploading-file-name-of-image-to-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Text file using Java</title>
		<link>http://www.bestjavainterviewquestions.com/522/</link>
		<comments>http://www.bestjavainterviewquestions.com/522/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 17:06:22 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Java Saving a File]]></category>
		<category><![CDATA[saving a file using java]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=522</guid>
		<description><![CDATA[//Write a java program for creating a text file import java.io.*; import java.io.OutputStream; class FileAccess { public static void main(String args[])throws IOException { //Attach keyboard to DataInputStream DataInputStream dis=new DataInputStream(System.in); //Attach myfile to fileoutputstream FileOutputStream fos=new FileOutputStream(&#8220;xxxx.txt&#8221;); //read data from datainputstream and write it into fileoutputstream ObjectOutputStream p=new ObjectOutputStream(fos); char ch; System.out.println(&#8220;Enter date(@ to end [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>//Write a java program for creating a text file </em></strong></p>
<p><em>import java.io.*;<br />
import java.io.OutputStream;</em></p>
<p><em>class FileAccess<br />
{<br />
public static void main(String args[])throws IOException<br />
{</em></p>
<p><em>//Attach keyboard to DataInputStream</em></p>
<p><em>DataInputStream dis=new DataInputStream(System.in);</em></p>
<p><em>//Attach myfile to fileoutputstream</em></p>
<p><em>FileOutputStream fos=new FileOutputStream(&#8220;xxxx.txt&#8221;);</em></p>
<p><em>//read data from datainputstream and write it into fileoutputstream</em></p>
<p><em>ObjectOutputStream p=new ObjectOutputStream(fos);<br />
char ch;<br />
System.out.println(&#8220;Enter date(@ to end the program);&#8221;);<br />
while((ch=(char)dis.read())!=&#8217;@')<br />
fos.write(ch);<br />
p.writeInt(12345);<br />
p.writeObject(&#8220;Today&#8221;);<br />
p.putFields();</em></p>
<p><em>//p.writeStreamHeader();</em></p>
<p><em>p.writeFields();</em></p>
<p><em>p.flush();</em></p>
<p><em>//closefikle</em></p>
<p><em>fos.close();<br />
}<br />
}</em></p>
<p><strong>Procedure:</strong></p>
<p>First compile the program and run the program then given the inputs as a string then exit that and u can fine the file in the destination u had specified</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/sockets-programming-in-java-for-clientserver/" title="Sockets programming in Java for Client/Server"><img src="Array" alt="Sockets programming in Java for Client/Server" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/difference-between-stateful-and-stateless-session-bean/" title="Difference Between StateFul and Stateless Session Bean"><img src="Array" alt="Difference Between StateFul and Stateless Session Bean" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/servlet-important-questions/" title="Servlet Important Questions"><img src="Array" alt="Servlet Important Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/ejb-interview-questions-2/" title="EJB Interview questions"><img src="Array" alt="EJB Interview questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/swings-interview-questions/" title="Swings Interview Questions"><img src="Array" alt="Swings Interview Questions" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/522/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explain Stream Tokenizer</title>
		<link>http://www.bestjavainterviewquestions.com/explain-stream-tokenizer/</link>
		<comments>http://www.bestjavainterviewquestions.com/explain-stream-tokenizer/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 08:27:33 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[StreamTokenizer]]></category>
		<category><![CDATA[define StreamTokenizer]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=499</guid>
		<description><![CDATA[Stream Tokenizer : StreamTokenizer is a direct subclass of Object class. StreamTokenizer is included in java.io. Package. The StreamTokenizer class takes an input stream and parses it into &#8220;tokens&#8221; , allowing the tokens to be read one at a time. The parsing process is controlled by a table and a number of flags ( like [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Stream Tokenizer :</strong></p>
<p>StreamTokenizer is a direct subclass of Object class. <em>StreamTokenizer is included in</em> <strong>java.io. Package</strong>.<em> The StreamTokenizer class takes an input stream and parses it into  &#8220;tokens&#8221; , allowing the tokens to be read one at a time. The parsing process is controlled by a table and a number of flags ( like TT_WORD, TT-EOF,TT-EOL etc., all representing some integer value) that can be set to various states. The StreamTokenizer can recongnize identifiers, numbers, quoted strings and various comment styles. Tokenizing is a feature of compilers, interpreters and parsers.</em></p>
<p>A stream can contain three types of tokens.</p>
<ul>
<li><em>Words ( that is, multiple character tokens )</em></li>
<li><em>Single-character tokens</em></li>
<li><em>Whitespace( including C/C++/Java-style comments )</em></li>
</ul>
<p>Some constants, defined in StreamTokenizer, used as  flags to identity the tokens :</p>
<p><em>int TT-EOL            : A constant indicating that the end of the line has been read.<br />
</em></p>
<p><em>Int TT-EOF            : A constant indicating that the end of the stream has been read.<br />
</em></p>
<p><em>int TT-WORD        : A constant indicating that a word token has been read.</em></p>
<p><em>Int ttype                :  After a call to the nextToken method, this field contains the type of the token just read</em></p>
<p><strong>Aim : </strong>To count the number of words in a file using StreamTokenizer and whitespace as delimiter File name is passes as command-line argument.</p>
<p><em><strong>Sample program of StreamTokenizer as follows</strong></em> :</p>
<p><em>import java.io.*<br />
public class StreamTokenizerDemo  {<br />
static int words = 0;<br />
public static void wordCount(Reader r)throws IOException<br />
{<br />
StreamTokenizer st = new StreamTokenizer(r);<br />
st.wordChars(33,255);</em></p>
<p><em>// if token in not EOF<br />
while(st.nextToken()!=st.TT_EOF)<br />
{<br />
//if token is word<br />
if(st.ttype == st.TT_WORD)<br />
words++;<br />
}</em></p>
<p><em>}<br />
public static void main(String args[])throws IOException<br />
{<br />
// pass file name as command-line<br />
FileReader fr = new FileReader(args[0]);<br />
wordCount(fr);<br />
System.out.pritnln(&#8221; Total words in file :&#8221;+words);<br />
}<br />
}</em></p>
<p><em><strong>Method signature of wordChars();</strong></em></p>
<p><em>public void wordChars(int low, int high);</em></p>
<p>Specifies that all characters between thew range low and high are word constituents. A word token consists of a word constituent followed by zero or more word constituents or number constituents</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/define-implicit-variables-in-jsp/" title="Define Implicit Variables in JSP"><img src="Array" alt="Define Implicit Variables in JSP" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/difference-between-print-and-println-methods-in-java/" title="Difference between Print() and Println() Methods in Java"><img src="Array" alt="Difference between Print() and Println() Methods in Java" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/creating-the-java-class/" title="Creating the Java Class"><img src="Array" alt="Creating the Java Class" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/explain-briefly-about-serialization/" title="Explain Briefly about Serialization"><img src="Array" alt="Explain Briefly about Serialization" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-jsp-include-and-jsp-forward/" title="Define Jsp-include and Jsp-forward"><img src="Array" alt="Define Jsp-include and Jsp-forward" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/explain-stream-tokenizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Define Static Keyword</title>
		<link>http://www.bestjavainterviewquestions.com/define-static-keyword/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-static-keyword/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 07:32:24 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Define static keyword]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=495</guid>
		<description><![CDATA[static is a keyword and means &#8216; can be accessed without the help of an object &#8216;. static is used an access modifier with members ( variables and methods ) of class. A static member belongs to the whole class. That is, all the objects of the class share ( or access ) the same [...]]]></description>
			<content:encoded><![CDATA[<p><strong>static is a keyword </strong>and means  <strong>&#8216; can be accessed without the help of an object &#8216;.</strong></p>
<p><strong>static </strong>is used an access modifier with members ( variables and methods ) of class. A static member belongs to the whole class. That is, all the objects of the class share ( or access ) the same static variable. Or to say, a static variable is common to all the objects of the class. static variables do not have &#8220;this&#8221; reference. <strong>&#8216; this &#8216; </strong>is a keyword used to refer to a particular current object. This is quite contrast with instance variables where each variable keeps a separate copy of data when called with an object. The static variables like instance variables are given default values ( like zero for int ), if not assiganed any values.</p>
<p><strong>Declaring static members : </strong></p>
<p><strong>static </strong>int count ;</p>
<p><strong>static </strong>int min( int a, int b)</p>
<p><em>{</em></p>
<p><em>//body</em></p>
<p><em>}</em></p>
<p><strong>Example :</strong></p>
<p><em>public class StaticDemo {<br />
static int salary = 5000;<br />
public static void display()<br />
{<br />
System.out.println(&#8220;Hello&#8221;);<br />
}<br />
public static void main(String args[])<br />
{<br />
System.out.println(salary);</em></p>
<p><em>//called directly without object<br />
StaticDemo sd= new StaticDemo();</em></p>
<p><em>//called with object<br />
System.out.println(sd.salary);</em></p>
<p><em>//called with class name<br />
System.out.println(StaticDemo.salary);</em></p>
<p><em>//all the below display calls prints Hello</em></p>
<p><em>display();<br />
sd.display();<br />
StaticDemo.display();<br />
}<br />
}</em></p>
<p><strong>Output :</strong></p>
<p><em>double d =10.5 ;<br />
int x=d;<br />
int y=(int)d ;<br />
System.out.println(y);</em></p>
<p>In the above, casting is done explicitly by us as we are assigning d of datatype double ( 8 bytes ) to x of datatype int  ( 4 bytes ).</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/uploading-file-name-of-image-to-database/" title="Uploading file name or image to DataBase"><img src="Array" alt="Uploading file name or image to DataBase" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/services-provided-by-ejb-container/" title="Services Provided by EJB Container"><img src="Array" alt="Services Provided by EJB Container" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-session-migration/" title="Define Session Migration"><img src="Array" alt="Define Session Migration" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions"><img src="Array" alt="Core Java Interview Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/how-the-web-container-creates-a-servlet-object/" title="How the Web Container creates a servlet object"><img src="Array" alt="How the Web Container creates a servlet object" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-static-keyword/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Abstract Class?Explan</title>
		<link>http://www.bestjavainterviewquestions.com/what-is-abstract-classexplan/</link>
		<comments>http://www.bestjavainterviewquestions.com/what-is-abstract-classexplan/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 07:01:30 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Explain Abstract Class]]></category>
		<category><![CDATA[define abstract class]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=492</guid>
		<description><![CDATA[Abstract Class  : In some situations we want to define a superclass that declares the structure of a given abstract without providing a complete implementation of every method. That means we only provide general code and it is developers responsibility to customize that class. To make sure this, we can make the abstract, that means [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract Class  :</strong></p>
<p>In some situations we want to define a superclass that declares the structure of a given abstract without providing a complete implementation of every method. That means we only provide general code and it is developers responsibility to customize that class. To make sure this, we can make the abstract, that means the developer will have to override your method .</p>
<p>The abstract keyword is used to make a method abstract if we have an abstract in a class, we also have to make the class an abstract.</p>
<p><strong>Note :</strong></p>
<p><strong>&#8211;&gt; </strong>An abstract class is a class that contain one or more abstract methods</p>
<p><strong>&#8211;&gt; </strong>An abstract class can have instance variables, concrete methods in addition to abstract methods</p>
<p><strong>&#8211;&gt; </strong>We cannot create object to abstract class</p>
<p><strong>&#8211;&gt; </strong>We can create reference variables to abstract class</p>
<p><strong>&#8211;&gt; </strong>All the abstract methods of abstract class should be implemented in its subclass.</p>
<p><strong>&#8211;&gt; </strong>A class cannot be both <em>abstract</em> and <em>final</em>.</p>
<p><strong>&#8211;&gt; </strong>Abstract class reference cannot refer to individual methods of the subclasses</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/some-important-methos-in-applets/" title="Some important methos in Applets"><img src="Array" alt="Some important methos in Applets" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-session-tracking-in-httpservlet/" title="Define Session Tracking in HttpServlet"><img src="Array" alt="Define Session Tracking in HttpServlet" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/explain-java-comments/" title="Explain Java Comments"><img src="Array" alt="Explain Java Comments" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-servletoutputstream/" title="Define ServletOutputStream"><img src="Array" alt="Define ServletOutputStream" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question"><img src="Array" alt="Basic Java Interview Question" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/what-is-abstract-classexplan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Define Array in Java</title>
		<link>http://www.bestjavainterviewquestions.com/define-array-in-java/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-array-in-java/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 17:25:24 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Define Array in java]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=481</guid>
		<description><![CDATA[An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as salary to represent a set of salaries of a group of employees. A particular value is indicated [...]]]></description>
			<content:encoded><![CDATA[<p>An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as <strong>salary</strong> to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called index number or subscript in brackets after the array name.</p>
<p><em> For example</em>:</p>
<p><em><strong>salary[3]</strong></em></p>
<p style="margin-bottom: 0in;">Represents the salary of 4<sup>th</sup> employee(array index starts from zero ). While the complete set of values are referred to as an array, the individual values are called elements. Arrays can be of any primitive data type.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>One-dimensional Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">A list of item can be given one variable name using only one subscript and such a variable is called as one-dimensional array.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>Creating an Array :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">Arrays must be declared and created in the computer memory before they are used. Creation of an array involves three steps;</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(i)    Declaring the array</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(ii)   Creating memory locations</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(iii)  Putting values in the memory locations</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>Declaration of Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">Arrays in java may be declared in two forms :</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;"><em>1<sup>st</sup> way : datatype arrayname[];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><em>2<sup>nd</sup> way : datatype[] arrayname;</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Examples :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;"><em>int number[];</em></p>
<p style="margin-bottom: 0in;"><em>float average[];</em></p>
<p style="margin-bottom: 0in;"><em>int[] counter;</em></p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>float[] marks;</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Note: </strong><span>The size of the array should not be mentioned while declaration</span></p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><span><br />
</span></p>
<p style="margin-bottom: 0in;"><strong>Creation of Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">After declaring an array, memory should be allocated. Java allows the creation of arrays using <strong>new </strong>keyword, as shown below:</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>type arrayname=new type[size];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Examples:</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;"><em>int number[];</em></p>
<p style="margin-bottom: 0in;"><em>number=new int[5];</em></p>
<p style="margin-bottom: 0in;"><em>float average[];</em></p>
<p style="margin-bottom: 0in;"><em>average=new float[10];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;">This lines create necessary memory locations for the arrays number and average and designate them as int and float respectively. Now, the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">It is also possible to combine both the steps&#8211; declaration and creation into aone as shown below.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>float average  =    new float[10];</em></p>
<p style="margin-bottom: 0in;"><em>int num []       =    new int[5];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong><em>Sample example of array&#8217;s in java are as follows;</em></strong></p>
<p style="margin-bottom: 0in;"><strong><em><br />
</em></strong></p>
<p style="margin-bottom: 0in;"><em>//java program for Total marks and percentage.</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><em>import java.io.*;<br />
class Arr<br />
{<br />
public static void main(String args[])throws IOException<br />
{<br />
//to accept data from keyboard</em></p>
<p style="margin-bottom: 0in;"><em>BufferedReader br=new BufferedReader(new InputStreamReader(System.in));<br />
System.out.println(&#8220;How many Subjects&#8221;);<br />
int n=Integer.parseInt(br.readLine());</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p><em>//create the array with size n<br />
int marks[] = new int[n];</em></p>
<p><em>//store marks into an array marks[]<br />
for(i=0;i&lt;n;i++)<br />
{<br />
System.out.println(&#8220;enter marks&#8221;);<br />
marks[i]=Integer.parseInt(br.readLine());<br />
}</em></p>
<p><em>//display the marks again and final total<br />
int tot=0;<br />
for(int i=0;i&lt;n;i++)<br />
{<br />
System.out.println(marks[i]);<br />
tot=tot+marks[i];<br />
}<br />
System.out.println(&#8220;total marks=&#8221;+tot);</em></p>
<p><em>//final percentage of marks</em></p>
<p><em>float percent=(float)total/n;</em></p>
<p><em>System.out.println(&#8220;percentage=&#8221;+percent);<br />
System.out.println(&#8220;arr size=&#8221;mark.length);<br />
}<br />
}<br />
</em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/explain-session-destroy-in-servlets/" title="Explain session destroy in Servlets"><img src="Array" alt="Explain session destroy in Servlets" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/deployment-descriptor/" title="Deployment Descriptor"><img src="Array" alt="Deployment Descriptor" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-session-hijacking-2/" title="Define Session Hijacking"><img src="Array" alt="Define Session Hijacking" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/how-application-creates-a-connection/" title="How application creates a connection"><img src="Array" alt="How application creates a connection" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-precompile-of-jsp-page/" title="Define Precompile of Jsp page"><img src="Array" alt="Define Precompile of Jsp page" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-array-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
